0
Node.js Rest API Scripting_language

Express js what and why

In a world of monolithic Web frameworks, enterprise-level solutions, and deep toolkits for every language, defaulting to the largest hammer for even the smallest nails is easy. Enter Express.js, described on its website as a “fast, un-opinionated, minimalist server-side Web framework for Node.js.”

Written in JavaScript, Express acts only as a thin layer of core Web application features. Unlike a big, highly opinionated framework like Ruby on Rails, Express has no out-of-the-box object relational mapping or templating engine.

Express isn’t built around specific components, having “no opinion” regarding what technologies you plug into it. It strives to put control in the developer’s hands and make Web application development for Node.js easier. This freedom, coupled with lightning fast setup and the pure JavaScript environment of Node, makes Express a strong candidate for quick development and rapid prototyping. Express is most popular with startups that want to build a product as quickly as possible and don’t have very much legacy code.

Let’s break down exactly how Express works. To understand Express, first you have to understand Node.js.

The Node.js environment is a key part of what makes Express so easy to build and deploy. Node can be thought of as a cross-platform JavaScript interpreter, able to run JavaScript free from the confines of the browser. In short, Node allows developers to write server-side code for Web apps and networking tools using JavaScript instead of another server-side language like PHP, Python, Java, etc. Using Node Package Manager, or npm, you can install libraries like Express to customize an existing Node installation to your needs. Node users have made thousands of these open-source libraries and server frameworks available. Check out this simple Web server

 

 

 

 

 

 

 

 

 

Why to use Express

It Supports

Parses the arguments and headers

Routing

Multiple view engines like Jade, EJS, JSHtml etc.

Handle Configurations

Sessions

Content Negotiation

Error Handling

Multiple Databases

RDBMS – MySQL, MS SQL etc.

NoSQL – MongoDB, Firebase, Redis etc

 

 

Http server using Express

 

npm install express

 

Code Snippet

 

‘use strict’;

//Basic call modules

const http = require(‘http’);

//Define the host name

const hostname = ‘127.0.0.1’;

//Define the port name

const port = 3000;

 

var express = require(“express”);

var router = express.Router();

var app = express();

 

//     apply the routes to our application

app.use(‘/’, router);

 

//route for home page

router.get(“/”, function (req, res)

{

res.statusCode = 200;//If get success show the status code

res.setHeader(‘Content-Type’, ‘text/plain’);

res.end(‘This is Home Page\n’);

 

});

//route for about us page

router.get(“/about”, function (req, res)

{

res.statusCode = 200;//If get success show the status code

res.setHeader(‘Content-Type’, ‘text/plain’);

res.send(“This is About Us Page”);

});

 

 

app.listen(port, hostname, () => {

console.log(`Server running at http://${hostname}:${port}/`);

});

 

Here is output

You Might Also Like...

No Comments

    Leave a Reply