a blog for those who code

Friday 8 January 2016

Getting Started with Express - Installation and Setup

In this post we will be discussing about getting started with Node.js framework Express. 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 is one of the most used web framework and some of the popular web application are using this framework.


Installation

There are two ways you can install the Express in your system i.e. globally and locally.

For globally : npm install -g express
For locally or In project installation : npm install --save express 


Along with the above installation we will also install some of the important modules to work with the Express Framework

express-generator : It use the application generator tool, express, to quickly create an application skeleton.
multer : It is a middleware for handling multipart/form-data which is primarily used for uploading files.
body-parser : It is a Node.js body parsing middleware which provides JSON body parser, Raw body parser, Text body parser and URL-encoded form body parser.
cookie-parser : It parse Cookie header and populate req.cookies with an object keyed by the cookie name.
express-session :  It is a middleware to handle Session in Express.

Setup

Next we are going to setup express project. Run command "express .", if express is not installed globally you might get an error like 'express' is not recognized as an internal or external command, operable program or batch file.


To fix the above error either you need to install express globally using command npm install -g express or you can create a cmd file (filename "express") with the below content in the same directory.

@IF EXIST "%~dp0\node.exe" (
  "%~dp0\node.exe"  "%~dp0\node_modules\express-generator\bin\express" %*) ELSE (
  @SETLOCAL
  @SET PATHEXT=%PATHEXT:;.JS;=;%
  node  "%~dp0\node_modules\express-generator\bin\express" %*
)

Then run "express .", it will ask you destination is not empty, continue? [y/N]. Type y. It will create your app with the following folder structure.


Then we need to run npm install to install the dependencies.

Run

To run the app use npm start and visit http://localhost:3000.



Please Like and Share the CodingDefined.com Blog, if you find it interesting and helpful.

No comments:

Post a Comment