HI WELCOME TO SIRIS

Install Express.js

Leave a Comment
Firstly, you have to install the express framework globally to create web application using Node terminal. Use the following command to install express framework globally.
  1. npm install -g express  
SettingServerExpressJS

Installing Express

Use the following command to install express:
  1. npm install express --save   
SettingServerExpressJS
The above command install express in node_module directory and create a directory named express inside the node_module. You should install some other important modules along with express. Following is the list:
  • body-parser: This is a node.js middleware for handling JSON, Raw, Text and URL encoded form data.
  • cookie-parser: It is used to 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.
  1. npm install body-parser --save   
SettingServerExpressJS
  1. npm install cookie-parser --save   
SettingServerExpressJS
  1. npm install multer --save   
SettingServerExpressJS

Express.js App Example

Let's take a simple Express app example which starts a server and listen on a local port. It only responds to homepage. For every other path, it will respond with a 404 Not Found error.
  1. File: express_example.js  
  1. var express = require('express');  
  2. var app = express();  
  3. app.get('/', function (req, res) {  
  4.    res.send('Welcome to JavaTpoint');  
  5. })  
  6. var server = app.listen(8000, function () {  
  7. var host = server.address().address  
  8.   var port = server.address().port  
  9.  console.log("Example app listening at http://%s:%s", host, port)  
  10. })  
SettingServerExpressJS
Open http://127.0.0.1:8000/ in your browser to see the result.
SettingServerExpressJS

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.