Learn the powerful enterprise adaptable database:

Getting Started With ADABAS & Natural

Friday, March 2, 2018

NodeJS Express Framework


Introduction

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:

  1. Allows to set up middlewares to respond to HTTP Requests.
  2. Defines a routing table which is used to perform different actions based on HTTP Method and URL.
  3. Allows to dynamically render HTML Pages based on passing arguments to templates.


How To Install Express?

  1. Run NodeJS command to install Express
    • e.g. sudo npm install express --save
    • The above command saves the installation locally in the node_modules directory and creates a directory express inside node_modules.
  2. Run NodeJS command to install important modules
    • e.g. sudo npm install body-parser --save
      • This is a node.js middleware for handling JSON, Raw, Text and URL encoded form data.
    • e.g. sudo npm install cookie-parser --save
      • Parse Cookie header and populate req.cookies with an object keyed by the cookie names.
    • e.g. sudo npm install multer --save
      • This is a node.js middleware for handling multipart/form-data.


Simple Demo

  1. Go to your target http root folder.
  2. Prepare server.js script file
    • var express = require('express');
    • var app = express();
    • app.get('/', function (req, res) {

    •    res.send('Hello World');
    • })

    • var server = app.listen(8081, function () {
    •    var host = server.address().address
    •    var port = server.address().port
    •    
    •    console.log("Example app listening at http://%s:%s", host, port)
    • })
  3. Type NodeJS command to start the server
    • e.g. sudo node server.js
  4. Browse the web page
    • e.g. http://localhost:8081/
    • This app responds with Hello World! for requests to the homepage. For every other path, it will respond with a 404 Not Found.
  5. To stop the server, press (in the console window):
    • CTRL-C


No comments:

Post a Comment