Express.js Framework

node+express

Prerequisites:

Before proceeding with this blog, you should have a installed Node.js 8.x or above version. If you don’t have installed please click here to install. To check Node.js version write node -v in terminal.

Introduction:

Express.js is a web application framework for the Node.js. It helps us to build a web application and APIs (Application Programming Interface).

Why Express.js ?

It is simplifying the code and helps us to build robust web application, because in Express.js build defined functions/methods and properties available like static(), Router(), urlencoded() and `req`, `res` which is helps us to build application easily.

Simple Node.js Program:

const http = require(‘http’);

http.createServer((req, res) => {
const url = req.url;
const method = req.method;
if (url === '/') {
res.write(`


Submit

`);
return res.end();
}

if (url === ‘/message’ && method === ‘POST’) {
const body = [];
req.on(‘data’, (chunk) => {
console.log(chunk);
body.push(chunk);
});

return req.on(‘end’, () => {
const parsedBody = Buffer.concat(body).toString();
const message = parsedBody.split(‘=’)[1];
console.log(message);
res.statusCode = 302;
res.setHeader(‘Location’, ‘/’);
return res.end();
});
}

res.setHeader(‘Content-Type’, ‘text/html’);
res.write(`

This is my First Page

`);
res.end();
}).listen(8080, () => {
console.log(`Server listen to 8080…`);
});

Here, we have used conditional routes maintained also difficulties to handle request data. While in express.js it will handle easily.
For running node.js application goes to project directory then write node index.js command from the terminal.

Node.js With Express.js Framework:

Run npm init command from terminal to create new Node.js project package.json file. Then create one javascript file that names `index.js` where you want to write your express web application. Second steps is to write below command to install framework. Which is store into `node_modules` directory.

npm install express --save

index.js

const express = require('express');
const app = express();
const path = require('path');
const port = process.env.PORT || 3000;
const host = process.env.HOST || 'http://localhost:';

app.get(‘/’, (req, res) => {
res.send(‘Hello World!’);
});

app.get(‘/api/numbers’, (req, res) => {
res.status(200).send([1, 2, 3]);
});

app.get(‘/api/numbers/:id’, (req, res) => {
res.status(200).send(req.params.id);
});

app.get(‘/api/posts/:year/:month’, (req, res) => {
res.send(req.params); // http://localhost:3000/api/posts/2018/12
res.send(req.query); // http://localhost:3000/api/posts/2018/12?q=raj
});

app.listen(port, () => {
console.log(`Server listen to ${host}${port}`);
});

Here, we can see the difference that the help of gat() method simplify the routes. Where first argument is string parameter second argument callback function. Which is easily maintainable for every route?

For running node.js application goes to project directory then write

node index.js command from the terminal.

Thank You…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.