Node.js - Express Framework
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. Following are some of the core features of Express framework −
- 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.
Firstly, install the Express framework globally using NPM so that it can be used to create a web application using node terminal.
The above command saves the installation locally in the node_modules directory and creates a directory express inside node_modules. You should 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.
Hello world Example
Following is a very basic Express app which starts a server and listens on port 3000 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.
Save the above code in a file named server.js and run it with the following command.
You will see the following output −
Open http://127.0.0.1:8081/ in any browser to see the following result.
Request & Response
Express application uses a callback function whose parameters are request and response objects.
- 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.
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).
We will extend our Hello World program to handle more types of HTTP requests.
You will see the following output −
Now you can try different requests at http://127.0.0.1:8081 to see the output generated by server.js. Following are a few screens hots showing different responses for different URLs.
Screen showing again http://127.0.0.1:8081/list_user
Screen showing again http://127.0.0.1:8081/abcd
Screen showing again http://127.0.0.1:8081/abcdefg
Serving Static Files
We will keep a few images in public/images sub-directory as follows −
Let's modify "Hello Word" app to add the functionality to handle static files.
Now open http://127.0.0.1:8081/images/logo.png in any browser and see observe following result.
Here is a simple example which passes two values using HTML FORM GET method. We are going to use process_get router inside server.js to handle this input.
Now you can enter the First and Last Name and then click submit button to see the result
Here is a simple example which passes two values using HTML FORM POST method. We are going to use process_get router inside server.js to handle this input.
The following HTML code creates a file uploader form. This form has method attribute set to POST and enctype attribute is set to multipart/form-data
File Upload: Select a file to upload:
NOTE: This is just dummy form and would not work, but it must work at your server.
You can send cookies to a Node.js server which can handle the same using the following middleware option. Following is a simple example to print all the cookies sent by the client.