Node JS Server with Express framework

Reading Time: 3 minutes

Introduction:

Express is a minimal and flexible Node.js web application framework that provides a robust set of features to develop web and mobile applications. It facilitates the rapid development of Node based Web applications. Following are some of the core features of Express framework −

  • Allows to set up middlewares to respond to HTTP Requests.
  • Defines a routing table which is used to perform different actions based on HTTP Method and URL.
  • Allows to dynamically render HTML Pages based on passing arguments to templates.

If you want to learn nodejs server with http, you can refer here.

Prerequisites:

To follow this blog along, prior knowledge of Node.js is essential.

Installing Express

Firstly, install the Express framework globally using NPM so that it can be used to create a web application using node terminal.

$ npm install express --save

The above command saves the installation locally in the node_modules directory and creates a directory express inside node_modules. You should install the following important modules along with express −

  • body-parser − This is a node.js middleware for handling JSON, Raw, Text and URL encoded form data.
  • cookie-parser − Parse Cookie header and populate req.cookies with an object keyed by the cookie names.
  • multer − This is a node.js middleware for handling multipart/form-data.
$ npm install body-parser --save
$ npm install cookie-parser --save
$ npm install multer --save
Now let’s create our first ‘Hello World’ using express framework in node.js

 Note: If you have Node and Express already installed, you can save this code in a text file called app.js and run it in a bash command prompt by calling:

node ./app.js

Following is a very basic Express app which starts a server and listens on port 3000 for connection. This app responds with Hello World! for requests to the homepage. For every other path, it will respond with a 404 Not Found.

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
  res.send('Hello World!')
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}!`)
});

The first two lines require() (import) the express module and create an Express application. This object, which is traditionally named app, has methods for routing HTTP requests, configuring middleware, rendering HTML views, registering a template engine, and modifying application settings that control how the application behaves (e.g. the environment mode, whether route definitions are case sensitive, etc.)

The middle part of the code (the three lines starting with app.get) shows a route definition. The app.get() method specifies a callback function that will be invoked whenever there is an HTTP GET request with a path ('/') relative to the site root. The callback function takes a request and a response object as arguments, and calls send() on the response to return the string “Hello World!”

The final block starts up the server on a specified port (‘3000’) and prints a log comment to the console. With the server running, you could go to localhost:3000 in your browser to see the example response returned.

Save the above code in a file named server.js and run it with the following command.
$ node server.js

After running the command, load http://localhost:3000/ in a browser to see the output. You should also see “Example app listening on port 3000!” get logged to the command line.

That’s it, you’re done. You have successfully created your first Node app using express framework. Don’t stop here, keep exploring the wonderful world of Node.js, as it has much more to offer.

Conclusion:

In this blog, we’ve learnt Node JS Server with Express framework. We’ve also seen how we can install them in the simple way. We’ve seen a simple program of express framework and it’s important feature. To conclude that, we can say it facilitates the rapid development of Node based Web applications

Hey there, I am glad you have reached the end of this post. If you liked this post or have some questions or want to discuss something let me know in the comment section. 

For more info you can check:

https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/Introduction

knoldus-advt-sticker

Written by 

Alka Vats is a Software Consultant at Knoldus. She is passionate about web development. She is recognized as a good team player, a dedicated and responsible professional, and a technology enthusiast. She is a quick learner & curious to learn new technologies. Her hobbies include reading books, watching movies, and traveling.

Leave a Reply