NodeJS/ExpressJS Middlewares

NodeJS/ExpressJS Middleware:

The order in which middleware are defined in a file tells the order in which they would be applied to an incoming request.

To install a middleware :: npm install middleware-name –save

1) morgan for logging.
app.use(morgan(‘dev’));

2) express.static for serving static content
app.use(express.static(__dirname + ‘/public’));

Static files requested can only be sent using the GET request not the POST request. Create a folder named ‘public’ with the static content inside.

3) body-parser for parsing the json or other content in body of request

eg. var bodyParser = require(‘body-parser’);
app.use(bodyParser.json());

4) Express Generator: Quick scaffolding tool to generate an Express application skeleton.
npm install express-generator -g

Go into the folder in which you want to create the node express application skeleton:
express app-name (generates the application skeleton).

Now install the dependencies from package.json
npm install

Now you can start the express server with :
npm start

Express Generator: http://expressjs.com/en/starter/generator.html

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s