Introduction
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 facilitates the rapid development of Node based Web applications.
Features:
- Allows to set up middlewares to respond to HTTP Requests.
- Defines a routing table which is used to perform different actions based on HTTP Method and URL.
- Allows to dynamically render HTML Pages based on passing arguments to templates.
How To Install Express?
- Run NodeJS command to install Express
- e.g. sudo npm install express --save
- The above command saves the installation locally in the node_modules directory and creates a directory express inside node_modules.
- Run NodeJS command to install important modules
- e.g. sudo npm install body-parser --save
- This is a node.js middleware for handling JSON, Raw, Text and URL encoded form data.
- e.g. sudo npm install cookie-parser --save
- Parse Cookie header and populate req.cookies with an object keyed by the cookie names.
- e.g. sudo npm install multer --save
- This is a node.js middleware for handling multipart/form-data.
Simple Demo
- Go to your target http root folder.
- Prepare server.js script file
- var express = require('express');
- var app = express();
- app.get('/', function (req, res) {
- res.send('Hello World');
- })
- var server = app.listen(8081, function () {
- var host = server.address().address
- var port = server.address().port
- console.log("Example app listening at http://%s:%s", host, port)
- })
- Type NodeJS command to start the server
- e.g. sudo node server.js
- Browse the web page
- e.g. http://localhost:8081/
- This app responds with Hello World! for requests to the homepage. For every other path, it will respond with a 404 Not Found.
- To stop the server, press (in the console window):
- CTRL-C
Originally from: https://www.tutorialspoint.com/nodejs/nodejs_express_framework.htm
No comments:
Post a Comment