How does Node.js Middleware Work?

How does Node.js Middleware Work?

You’re running a web application on a web server with Node.js and Express. In this application, let’s say your certain pages that require that you log in.

What is Express middleware?
  • Middleware literally means anything you put between one layer and another layer of software.
  • Express middleware is a function that executes during the life of a request to an Express server.
  • Each middleware has access to all the HTTP requests and responses to which it is attached.
  • Alternatively, the middleware can terminate the HTTP request or pass it to another middleware function with next. This "chain" of middleware allows you to partition your code and create reusable middleware.
Requirements for writing Express middleware

You need to install something to create, use, and test Express middleware. Node and NPM are needed first. To ensure it is installed, you can run:

npm -v && node -v

You should see the Node and NPM versions installed. If something goes wrong, you need to install Node . All examples should be used under Node ver 8+ and NPM ver 5+.

This article uses Express 4.x. This is important because of major changes from 3.x to 4.x.

Express Middleware: Basic

First we use Express's most basic built-in middleware. Create a new project and initialize it with npm ...

npm init
npm install express --save

Create server.js and paste the following code:

const express = require('express');
const app = express();

app.get('/', (req, res, next) => {
  res.send('Welcome Home');
});

app.listen(3000);
What problems does middleware solve? Why use it?

Suppose you are using Node.js and Express to run a web application on a web server . In this application, you need to log in to certain pages.

When the web server receives a data request, Express provides you with a request object that contains information about the user and the data they requested. Express also gives you access to the response object, which can be modified before the web server responds to the user. These objects are usually shortened to req , res.

Middleware functions are an ideal place to modify the req and res objects with related information. For example, users log in, you can get detailed information from its user database, and then these details are stored in res.user the.

What does a middleware function look like?
async function userMiddleware (req, res, next) {
    try {
        const userData = await getUserData(req.params.id);  //see app.get below

        if(userData) {
                req.user = userData;
                next();
        }
    } catch(error)  {
        res.status(500).send(error.message); //replace with proper error handling
    }
}

If an error occurs and you do not want to execute other code, do not call this function. Remember to send a response in this case, otherwise the client will wait for the response until it times out.

var app = express ();

//your normal route Handlers
app.get('/user/:id', userMiddleware, userController);
Middleware chain

You can forward or by using multiple arrays in the middleware app.use to link call middleware:

app.use(middlewareA);
app.use(middlewareB);
app.get('/', [middlewareC, middlewareD], handler);

After Express receives the request, each middleware that matches the request will run in the initialization order until there is a terminating operation.

Therefore, if an error occurs, all middleware for error handling will be called in order until one of them no longer calls the next() function call.

Express middleware types
  • Router-level middleware, such as router.use

  • Built-in middleware, such as: express.static, express.json, express.urlencoded

  • Error handling middleware, for example: app.use (err, req, res, next)

  • Third-party middleware, such as: bodyparser, cookieparser

  • Router-level middleware

  • express.Router uses the express.Router class to create modular, installable route processing. The routing instance is a complete middleware and routing system.

    • You can use middleware for logging, authentication, etc. As shown below, to record the user's latest activity and parse the authentication header, use it to determine the currently logged in user and add it to the Request object.
    • This function is executed every time the program receives a request. If there is an error, it simply ends the response without invoking subsequent middleware or routing processing.
var router = express.Router()
//Load router-level middleware by using the router.use() and router.METHOD() functions.
//The following example creates a router as a module, loads a middleware function in it,
//   defines some routes, and mounts the router module on a path in the main app.
var express = require(‘express’);
var router = express.Router();

// a middleware function with no mount path. This code is executed for
//   every request to the router
// logging
async function logMiddleware (req, res, next) {
    try {
         console.log(req.user.id, new Date());
     next();
    } catch() {
        res.status(500).send(error.message);
    }
}
// authentication
    async function checkAuthentication(req, res, next) => {
// check header or url parameters or post parameters for token
const token = req.body.token || req.query.token || req.headers['x-access-token']
 || req.headers['authorization'];
      if (token) {
        try {
            // verifies secret
            req.decoded = await jwt.verify(token, config.secret)

            let checkUser = await authenticateTokenHelper.getUserDetail(req);

            // if everything is good, save to request for use in other routes
                if (checkUser) {
                        req.user = req.decoded
                        next()
                } else {
                    return res.status(403).json({ 
                    message: responseMessage.noAuthorized 
                    })
                }
        } catch (err) {
            return res.status(401).json({ message: responseMessage.invalidToken })
        }
  } else {
    // if there is no token
    return res.status(400).json({ message: responseMessage.invalidRequest })
  }
}
router.use(logMiddleware);
    router.get('/user, checkAuthentication, handler);
Built-in middleware

Express has the following built-in middleware features:

  • express.static Provide static resources such as HTML files, images, etc.
  • express.json The payload parses the incoming request in JSON.
  • express.urlencoded Parse incoming URL-encoded payload requests.
Error handling middleware

Error handling middleware always takes four parameters (err, req, res, next). You must identify it as an error-handling middleware function by providing four parameters. It must be specified even if you don't need to use the next object. Otherwise, the next object will be interpreted as regular middleware and will not be able to handle errors. The basic signature looks like this:

app.use(function (err, req, res, next) {
  console.error(err.stack)
  res.status(500).send('Something broke!')
})

Example 1:

app.get('/users', (req, res, next) => {
  next(new Error('I am passing you an error!'));
});
app.use((err, req, res, next) => {
  console.log(err);    
  if(!res.headersSent){
    res.status(500).send(err.message);
  }
});

In this case, the error handling middleware at the end of the pipeline will handle the error. You may also notice, I checked the res.headersSent property. This just checks if the response has sent the header to the client. If not, it will send HTTP 500 status and error messages to the client.

Example 2:

You can also link error handling middleware. Different types of errors are usually handled differently:

app.get('/users, (req, res, next) => {
  let err = new Error('I couldn\'t find it.');
  err.httpStatusCode = 404;
  next(err);
});

app.get('/user, (req, res, next) => {
  let err = new Error('I\'m sorry, you can\'t do that, Dave.');
  err.httpStatusCode = 304;
  next(err);
});

app.use((err, req, res, next) => {
   // handles not found errors
  if (err.httpStatusCode === 404) {
    res.status(400).render('NotFound');
  }
   // handles unauthorized errors 
  else if(err.httpStatusCode === 304){
    res.status(304).render('Unauthorized');
  }
    // catch all
   else if (!res.headersSent) {
     res.status(err.httpStatusCode || 500).render('UnknownError');
  }
  next(err);
});
  • In this case, the middleware checks if a 404 (not found) error is thrown. If it is, it renders the "NotFound" template page and then passes the error to the next item in the middleware.
  • The next middleware checks if a 304 (unauthorized) error is thrown. If it is, it will render the "Unauthorized" page and pass the error to the next middleware in the pipeline.
  • Finally, the "catch all" error handling only logs errors, if no response is sent, it sends the error httpStatusCode (or HTTP 500 status if not provided) and renders the "UnknownError" template.
Third-party-level middleware

In some cases, we will add some extra features to the backend. Install the Node.js module to get the required functionality, and then load it into your application at the application level or router level.

Example: When the body-parser processing request Content-Type header, all intermediate filling body will use the parsed req.body attribute.

const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({extended:false}))
app.use(bodyParser.json())
app.post('/save',(req,res)=>{
    res.json({
        "status":true,
         "payload":req.body
    })
}
app.listen(3000,(req,res)=>{
    console.log('server running on port')
})
to sum up

Middleware functionality is a great way to run code on every request or every request for a specific route, and take action on the request or response data. Middleware is an important part of modern web servers and is very useful.

NodeJS API Development with Express MongoDB and Mongoose

NodeJS API Development with Express MongoDB and Mongoose

NodeJS API Development with Express MongoDB and Mongoose. MongoDB is a NoSQL document-oriented database. It’s popular in the Node.js community and a viable database solution for building real-world applications.

MongoDB is different from traditional, SQL databases like MySQL and PostgreSQL in that data is stored in binary JSON-like objects called BSON). This structure lends itself well to building Javascript applications that communicate with JSON. Additionally, MongoDB has flexible schema. This means there aren’t database migrations to worry about and data models can grow and change.

In this tutorial, we’re going to set up a Node.js server application, connect it to MongoDB and demonstrate how relationships work between MongoDB Collections. In the table below (provided by MongoDB) you’ll see how traditional aspects of SQL databases stack up against their MongoDB equivalents. You can find the whole source code for this tutorial in this GitHub repo.

In SQL databases, we get database relationships using joins. For example, if we had a SQL database with two tables, books and authors, we could get all the books that belong to an author like so:

SELECT b.id AS ‘Post ID’, 
b.title AS ‘Book Title’, 
a.name AS ‘Author Name`, 
a.id AS ‘Author ID’
FROM books b
JOIN authors ON b.author_id = a.id
WHERE a.id = 1234;

This will grab information from both tables and display the results in a single dataset for us. Frameworks like Ruby On Rails and Laravel have abstracted this functionality for developers, making it possible to write PHP or Ruby to grab related information.

In Ruby On Rails, using Active Record finding, an author and related posts could look like:

authorWithBooks = Author.find 1234, :include => [:books]

In Laravel, using Eloquent, we could do:

$authorWithBooks = Author::find(1234)->books();

These results would give us the author with id 1234 and all the books that they've written. On the books table, we'd store an author_id, setting up the relationship between authors and books in the SQL world. MongoDB doesn't use joins though, so how do we achieve this functionality?

There is a helper npm package for working with MongoDB called mongoose that we’re going to use for illustrative purposes in this tutorial. Mongoose is an ORM (stands for Object Relationship Mapper) that is a helper for MongoDB kind of like how ActiveRecord and Eloquent are helpers for working with relational data.

Create Database Models with Mongoosejs

The first thing to do is set up our models in Mongoose. These schemas are flexible but help us define what we want our data to look like.

For the author model, we define a model schema that can reference documents in another collection:

const mongoose = require('mongoose');
const authorModel = mongoose.Schema({
  name: { 
   type: String, 
   required: '{PATH} is required!'
  },
  bio: {
   type: String
  },
  website: {
   type: String
  },
  books: [
    { type: mongoose.Schema.Types.ObjectId, ref: 'Book' }
  ]
}, {
  timestamps: true
});
module.exports = mongoose.model('Author', authorModel);

In the above model, we define that in the authors MongoDB collection authors have name, bio, website and an array of books. Each element in the books array will reference the book id on the books collection. We’ll define that below. The second argument, saying timestamps = true will include "updated at" and "created at" fields when we create author records.

The Books schema models what our book data will look like. The schema has a reference to find the id of an associated author. In this example, I’m saying that a book is written by only one author, though in the real world that’s not always the case! Here’s what a belongs-to relationship could look like using Mongoose.js:

const mongoose = require('mongoose');
const bookModel = mongoose.Schema({
  title: { 
    type: String, 
    required: '{PATH} is required!'
  },
  subtitle: {
    type: String
  },
  author: { 
    type: mongoose.Schema.Types.ObjectId, 
    ref: 'Author' 
  }
}, {
  timestamps: true
});
module.exports = mongoose.model('Book', bookModel);

Instead of an array of authors, the book references one author id as the author of the book. We’re using timestamps again for the “updated at” and “created at fields”.

In the root models directory, I added an index to register the models:

module.exports = {
  'Author': require('./Author'),
  'Book': require('./Book'),
};
Register Routes to Return JSON From Express 4

Now that we have the authors and book models defined, it’s time to return and show the data via a JSON API. For that, I set up a controllers for Authors called AuthorsController and one for Books called BooksController. The controllers are responsible for handling the request after the router determines which route to use. Below, we'll define a method for rendering a JSON response of all authors and the JSON of one author based on an id.

The authors controller looks like this:

const { Author } = require('../models');
const AuthorsController = {
  async index(req, res){
    const authors = await Author
       .find()
       .populate('books');
    res.send(authors);
  },
  async show(req, res){
    const author = await Author
       .findById(req.params.id)
       .populate(‘books’);
    res.send(author);
  }
};
module.exports = AuthorsController;

Here, I’m importing the author model, grabbing all of them and populating the query result with the related books. To use the async-await functionality with Express 4, I pulled in a package called express-async-errors and registered it in like so: require('express-async-errors');.

Following that Express 4 requires some server boilerplate setup:

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
app.use(methodOverride());
app.use(cookieParser());
app.use(express.static(__dirname + '/public'));
require('./server/routes')(app);

In the **server/routes.js** file, I register API routes for showing all the authors with their books and individual author with their books:

const express = require('express'),
  path = require('path'),
  rootPath = path.normalize(__dirname + '/../'),
  router = express.Router(),
  { AuthorsController, 
    BooksController } = require('./controllers');
module.exports = function(app){
  router.get('/authors', AuthorsController.index);
  router.get('/authors/:id', AuthorsController.show);
  app.use('/api', router);
};

Now we have a working API that returns authors with the books that they’ve written. The only problem is that there are no authors or books stored in MongoDB yet! To fix that, we’ll need to set up code to seed the database with records. If you visit /api/authors now all you'll see is an empty array.

Seed Records Into MongoDB

We need to make sure that the Express 4 server connects properly to MongoDB. For that, we can connect via an URL and listen for successful connection events like so:

const mongoose = require('mongoose'),
  env = process.env.NODE_ENV = process.env.NODE_ENV || 'development',
  envConfig = require('../server/env')[env];
mongoose.Promise = require('bluebird');
mongoose.connect(envConfig.db, { useMongoClient: true, });
mongoose.connection.on('connected', function () {  
  console.log(`Database connection open to ${mongoose.connection.host} ${mongoose.connection.name}`);
});
mongoose.connection.on('error',function (err) {  
  console.log('Mongoose default connection error: ' + err);
});
mongoose.connection.on('disconnected', function () {  
  console.log('Mongoose default connection disconnected'); 
});

With the environment config file defined like so:

var path = require('path'),
  rootPath = path.normalize(__dirname + '/../../');
  
module.exports = {
  development: {
    rootPath: rootPath,
    db: 'mongodb://localhost/mongodb-relationships',
    port: process.env.PORT || 3000
  },
  production: {
    rootPath: rootPath,
    db: process.env.MONGOLAB_URI || 'you can add a mongolab uri here ($ heroku config | grep MONGOLAB_URI)',
    port: process.env.PORT || 80
  }
};

The seeder itself we’re going to run from the command line. It’s a bit verbose but goes through the process of creating and updating records in MongoDB with Mongoose.js.

require('./index');
const mongoose = require('mongoose');
const { Author, Book } = require('../server/models');
async function seedAuthors() {
  console.log('Seeding authors to ' + mongoose.connection.name + '...');
  const authors = [
    { name: 'JK Rowling', bio: 'J.K. Rowling is the author of the much-loved series of seven Harry Potter novels, originally published between 1997 and 2007.' },
    { name: 'Tony Robbins', bio: 'Tony Robbins is an entrepreneur, best-selling author, philanthropist and the nation\'s #1 Life and Business Strategist.' },
  ];
for (author of authors) {
    var newAuthor = new Author(author);
    await newAuthor.save();
  }
const a = await Author.find();
  console.log('authors: ', a);
}
async function seedBooks() {
  console.log('Seeding books to ' + mongoose.connection.name + '...');
const jkRowling = await Author.findOne({ name: 'JK Rowling' });
  const tonyRobbins = await Author.findOne({ name: 'Tony Robbins' });
let harryPotter = new Book({ title: 'Harry Potter', author: jkRowling._id });
  let awakenGiant = new Book({ title: 'Awaken the Giant Within', author: tonyRobbins._id });
await harryPotter.save();
  await awakenGiant.save();
jkRowling.books.push(harryPotter);
  tonyRobbins.books.push(awakenGiant);
await jkRowling.save();
  await tonyRobbins.save();
}
seedAuthors();
seedBooks();

This will create a new connection to the MongoDB database and then convert a normal array of JavaScript objects into data we can persistently access. The author will have an array of books with one book for each author in the array. To add more books, we can push to the books array and save the changes. Each book will have one author. MongoDB stores these relationships via the id. Using the populate method in our controller above, we'll be able to view the entire object.

After running the seeder, you should be able to see your records in MongoDB Compass, as shown below. Compass is a GUI for viewing, creating, deleting, querying and editing MongoDB data.

Test The API

Now, to view this data from MongoDB via the API, start the Node server with npm run start and visit localhost:3000/api/authors in the web browser.

The final data will look something like:

[ 
   { 
      "_id":"5d51ea23acaf6f3380bcab56",
      "updatedAt":"2019-08-12T22:38:46.925Z",
      "createdAt":"2019-08-12T22:37:23.430Z",
      "name":"JK Rowling",
      "bio":"J.K. Rowling is the author of the much-loved series of seven Harry Potter novels, originally published between 1997 and 2007.",
      "__v":1,
      "books":[ 
         { 
            "_id":"5d51ea76f607f9339d5a76f6",
            "updatedAt":"2019-08-12T22:38:46.919Z",
            "createdAt":"2019-08-12T22:38:46.919Z",
            "title":"Harry Potter",
            "author":"5d51ea23acaf6f3380bcab56",
            "__v":0
         }
      ]
   },
   { 
      "_id":"5d51ea23acaf6f3380bcab57",
      "updatedAt":"2019-08-12T22:38:46.937Z",
      "createdAt":"2019-08-12T22:37:23.475Z",
      "name":"Tony Robbins",
      "bio":"Tony Robbins is an entrepreneur, best-selling author, philanthropist and the nation's #1 Life and Business Strategist.",
      "__v":1,
      "books":[ 
         { 
            "_id":"5d51ea76f607f9339d5a76f7",
            "updatedAt":"2019-08-12T22:38:46.921Z",
            "createdAt":"2019-08-12T22:38:46.921Z",
            "title":"Awaken the Giant Within",
            "author":"5d51ea23acaf6f3380bcab57",
            "__v":0
         }
      ]
   }
]

Congratulations, you’ve built an API with Node.js, Express 4 and MongoDB!

Lastly, a word from the Jscrambler team — before shipping your web apps, make sure you are protecting their JavaScript source code against reverse-engineering, abuse, and tampering. 2 minutes is all it takes to begin your free Jscrambler trial and start protecting JavaScript.

React, NodeJS, Express & MongoDB - The MERN Stack Guide for Beginners

React, NodeJS, Express & MongoDB -  The MERN Stack Guide for Beginners

ReactJS and NodeJS, together with ExpressJS & MongoDB form the very popular MERN stack! In this video guide you Building fullstack applications (i.e. frontend + backend) with the MERN stack is very popular - in this course, you will learn it from scratch at the example of a complete project!

MERN stands for MongoDB, Express.js, React.js and Node.js - and combined, these four technologies allow you to build amazing web applications.

Output in browser using Express Nodejs

I install nodejs with express in server (subdirectory in ftp,path is /var/www/html/admin) and in admin folder i created file "app.js" which is working in xshell fine and showing message in console

I install nodejs with express in server (subdirectory in ftp,path is /var/www/html/admin) and in admin folder i created file "app.js" which is working in xshell fine and showing message in console

here is my app.js

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => res.send('Hello World!'))

app.listen(port, () => console.log(Example app listening on port ${port}!))

but now i want to show result in browser,how can i do this i tried with following urls but not working for me

http://myurl.com/admin:3000
http://myurl.com/3000/admin