In this post I would like to write getting started steps for Express.js framework.
What is Express? Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.It’s an easy to use node.js framework for web applications.
How to Write Middleware for Express.js Apps
Installing Express.js
Let’s begin by installing it(assuming we already have Node.js installed). First, create a directory to hold your application, if you have not already done create a directory.I named my directory as “myapp”.
1 2 |
$ mkdir myapp $ cd myapp |
Now open your terminal, change to it and run npm init. init command will generate a package.json file in the current directory in our case in myapp.
1 |
npm init |
After running ‘npm init’ you’ll be prompted with several questions. You can fill them out or hit enter to get through it. Now our package.json is ready now install Express in the “myapp” directory and save the dependencies and update the package.js file with dependencies list:
1 |
npm install express --save |
To install Express temporarily(don’t save the dependencies list) omit the “–save” option.
To verify our Express installation just create a file named app.js and add the following code:
1 2 3 4 5 6 7 8 9 10 |
var express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Hello World!'); }); app.listen(3000, function () { console.log('Example app listening on port 3000!'); }); |
We have created a new server using ‘app.listen’ and setting the http GET on app to send a ‘Hello World’ message. Other than root URL (/) and For every other path, it will respond with a 404 Not Found.
Now we need to start the server.
1 |
node app.js |
Awesome! Open a browser and head for http://localhost:3000 where you should see the ‘Hello World’ message.
I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.
I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.