NodeJs – Express Framework

Express.js is a framework used for nodejs to build a web based application . Express is just a module framework for nodejs to build a web based application that are based on server that will handles any kind of input / output request handling from clients .

Difference Between NodeJs and expressJs :

Nodejs is a platform building server side Event driven I/O application based on javascript.

Express.js is a framework used for building a web based application with the principles and approaches within a nodejs application .

Features of Express :

This framework provides the various features that allows you to create web application easy and fast otherwise it takes more time to develop the same feature using nodejs alone.

Express js is based on the nodejs Middleware called connect . this connect uses the Http module.

Install Express.js :

you can install express js using NPM .The below code will install the latest version of the express js globally in your system so that you can use this feature in all nodejs application.

npm install -g express

If you want install the package locally in your project or save in package.json file . run the below code .

 C:\nodeproject > npm install express --save

The above command install the package code locally in your node_modules directory .You can install following important modules along with express .

body-parser This is a nodejs Middleware for handling the JSON ,Raw Text and URl Encoded.
cookie-parser This is used to parse the cookie header and populate the req.cookies with object keyed by the cookie name.
multer This is nodejs middleware used for handling the multipart and form data.
npm install body-parser --save
npm install cookie-parser --save
npm install multer --save

we will discuss how the above modules implemented in a project development in the upcoming topics.

Hello World Program using Express

Simple express app to Start the server and listen to the port for connection on 8085.

var express = require('express');
var app = express();

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

var server = app.listen(5050, function () {
   var host = server.address().address
   var port = server.address().port
   
   console.log("Example app listening at http://%s:%s", host, port)
})

Save the above code in your project folder using the express_server.js and Run the file using below code.

node express_server.js

Leave a Reply

Your email address will not be published. Required fields are marked *