What is Express ?
Express is a fast, assertive, essential and moderate web framework of Node.js. You can assume express as a layer built on the top of the Node.js that helps manage a server and routes. It provides a robust set of features to develop web and mobile applications.
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 of Express framework
Some of the core features of Express framework:
It can be used to design single-page, multi-page and hybrid web applications.
It allows to setup middlewares to respond to HTTP Requests.
It defines a routing table which is used to perform different actions based on HTTP method and URL.
It allows to dynamically render HTML Pages based on passing arguments to templates.
Installing Express
To install the Express framework globally using NPM so that it can be used to create a web application using node terminal.
$ npm install express --save
The above command saves the installation locally in the node_modules directory and creates a directory express inside node_modules. You have to install the following important modules along with express:
body-parser: This is a node.js middleware for handling JSON, Raw, Text and URL encoded form data.
cookie-parser: 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.
$ npm install body-parser --save $ npm install cookie-parser --save $ npm install multer --save
Hello World Express
Here, we will learn with an example of Express app which starts a server and listens on port 8012 for connection. This app responds with Hello World!!! for requests to the homepage. For every other path, it will respond with a 404 Not Found.
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!!!');
})
var server = app.listen(8012, function () {
var host = server.address().address
var port = server.address().port
console.log("You are listening app at http://%s:%s", host, port)
})
We will save the above code in a file and name it server.js and then run it by using the following command:
$ node server.js
You can see the following output:
You are listening app at http://0.0.0.0:8012
You have to open http://127.0.0.1:8081/ in any browser to see the following result.
IMAGE
Request & Response
Express application uses a callback function whose parameters are request and response objects.
app.get('/', function (req, res) { // -- })
Request Object: The request object represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, and so on.
Response Object: The response object represents the HTTP response that an Express app sends when it gets an HTTP request.
One can print req and res objects which provide a lot of information related to HTTP request and response including cookies, sessions, URL, etc.
Basic Routing
We have seen a basic application which serves HTTP request for the homepage. Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, and so on).
Now, we will add some more codes in our Hello World program to handle more types of HTTP requests.
var express = require('express');
var app = express();
// This responds with "Hello World" on the homepage
app.get('/', function (req, res) {
console.log("Got a GET request for the homepage");
res.send('Hello GET');
})
// This responds a POST request for the homepage
app.post('/', function (req, res) {
console.log("Got a POST request for the homepage");
res.send('Hello POST');
})
// This responds a DELETE request for the /del_user page.
app.delete('/del_user', function (req, res) {
console.log("Got a DELETE request for /del_user");
res.send('Hello DELETE');
})
// This responds a GET request for the /list_user page.
app.get('/list_user', function (req, res) {
console.log("Got a GET request for /list_user");
res.send('Page Listing');
})
// This responds a GET request for abcd, abxcd, ab123cd, and so on
app.get('/ab*cd', function(req, res) {
console.log("Got a GET request for /ab*cd");
res.send('Page Pattern Match');
})
var server = app.listen(8012, function () {
var host = server.address().address
var port = server.address().port
console.log("You are listening app at http://%s:%s", host, port)
})
We will save the above code in a file and then name it server.js and then run it with the following command:
$ node server.js
We can see the following output:
Example app listening at http://0.0.0.0:8012
Now we can try different requests at http://127.0.0.1:8012 to see the output generated by server.js.
Serving Static Files
Express provides a built-in middleware express.static to serve static files, such as images, CSS, JavaScript, etc.
We can simply need to pass the name of the directory where we keep our static assets, to the express.static middleware to start serving the files directly. For example, if we have kept our images, CSS, and JavaScript files in a directory named public, we can do this:
app.use(express.static('public'));
We can also keep a few images in public/images sub-directory as follows:
node_modules
server.js
public/
public/images
public/images/logo.png
If you have any queries regarding this blog or need any help you can contact us on: contact@codersarts.com
Node.js Coders, Node.js Experts, Node.js Tutors, Node.js Specialists, Node.js Programmers, Node.js Coding Help, Node.js Programming Help, Node.js Assignment Help, Node.js Engineers, Node.js Consultants, Node.js Development Company, Node.js Express Server
Comments