Ans: Express.js is a light-weight node.js based web application framework. This JavaScript framework provides a number of flexible and useful feature in order to develop mobile as well as web application using NodeJS.
Ans: Use following command to install express js. :
npm install express
Ans: You can build single-page, multi-page, and hybrid web applications.
Ans: You should use Express because provides following features:
Ans: Following are some of the core features of Express framework:
Ans: Express is best for developing web application using Node.js. You can create an http server using express as given below:
var express = require("express" );
var app = express();
app.get( "/", function (req, res) {
res.write("Hello, Express");
res.end();
});
var port = process.env.port || 1305;
app.listen(port);
console.log("Server is running at http://localhost:" + port);
Ans: Assuming you’ve already installed Node.js, create a directory to hold your application, and make that your working directory.
$ mkdir myapp
$ cd myapp
Use the npm init command to create a package.json file for your application. For more information on how package.json works, see Specifics of npm’s package.json handling.
$ npm init
This command prompts you for a number of things, such as the name and version of your application. For now, you can simply hit RETURN to accept the defaults for most of them, with the following exception:
entry point: (index.js)
Enter app.js, or whatever you want the name of the main file to be. If you want it to be index.js, hit RETURN to accept the suggested default file name.
Now install Express in the myapp directory and save it in the dependencies list. For example:
$ npm install express --save
To install Express temporarily and not add it to the dependencies list, omit the --save option:
$ npm install express
Ans: GET : Used to read.
POST: Used to update.
PUT: Used to create.
DELETE: Used to delete.
Ans: Use require to include express module.
var app = require('express')();
Ans: /*Include require module*/
var app = require('express')();
var http = require('http').Server(app);
app.get('/', function (req, res) {
console.log("Got a GET request for the homepage");
//Shown in console res.send('This is GET Method for Homepage'); //
Display as response
})
/*Start listing 8080 port*/
http.listen('8080', function() {
console.log('listening on *:8080');
});
Ans: The arguments available to an Express.js route handler function are:
The third argument may be omitted, but is useful in cases where you have a chain of handlers and you would like to pass control to one of the subsequent route handlers, and skip the current one.
Ans: The popular template engines which you can use with Express are Pug, Handlebars, Mustache, and EJS. The Express application generator uses Pug as its default template engine.
Ans: app.get('/download', function(req, res){
var file = __dirname + '/download-folder/file.txt';
res.download(file);
});
Ans: var express = require('express');
var app = express();
app.get('/', function(req, res){
/* req have all the values **/
res.send('id: ' + req.query.id);
});
app.listen(3000);
Ans: var bodyParser = require('body-parser') app.use( bodyParser.json() );
// to support JSON-encoded
app.use(bodyParser.urlencoded({
// to support URL-encoded
extended: true
}));
Ans: app.get('/userdetails/:id?', function(req, res, next){ });
req and res which represent the request and response objects
nextIt passes control to the next matching route.
Ans: Parse incoming request bodies in a middleware before your handlers, available under the req.body property.
To handle HTTP POST request in Express.js version 4 and above, you need to install middleware module called body-parser.
body-parser extract the entire body portion of an incoming request stream and exposes it on req.body.
Installation
$ npm install body-parser
API
var bodyParser = require('body-parser')
Example
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
app.use(function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.write('you posted:\n')
res.end(JSON.stringify(req.body, null, 2))
})