#
Express.js framework & Node.js
This tutorial explains to you what Express.js framework is and how you can use it with Node.js. This article provides you with an example as well.
Express is one of the best frameworks for Node.js. Express is a fast, robust and asynchronous
framework like Node.js.
Express framework can be used to:
- create single-page, multi-page and hybrid web applications
- setup middleware engines to respond to HTTP Requests
- define a routing table which is used to perform different actions based on HTTP method and URL
- create dynamic HTML Pages based on passing arguments to templates
- work with databases (SELECTs, UPDATEs, INSERTs, DELETEs, etc)
Express is a powerful and efficient server-side framework. Angular is a powerful and efficient framework too, but it is built to run on the client side.
Express is a package for Node.js. You can install the Express into Node.js using the Node Package Manager (NPM):
Info
- When you create an application, you have to create a folder for it. In my case I created the folder named "express-app" on my desktop.
- Express can be installed using the command rpm install express
- When you install express it is a good thing to install also body-parser, cookie-parser and multer
Info
body-parser
-> 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
-> for handling multipart/form-data.
Here it is a Hello World example written in Express:
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.listen(2000, function () {
console.log('Example app listening on port 2000!');
});
When you run you see the following into the console:
In the browser you can see:
In the browser you can see also on another URL defined in Express application:
So, Express is a nice framework for creating web and middleware applications, and in the same time keeping the advantages of Node.js.