Expressjs routing setup with nodejs and mongodb

node.js express mongodb crud
creating a rest api using node.js express and mongodb
creating a rest api using node.js express and mongodb github
node js mongodb tutorial
search api in node js mongodb
node js rest api mongodb example
build a blog with node js express and mongodb
mongodb rest api

I'm new to expressjs and nodejs. I'm working on a MEAN app, and I'm facing issues setting up the routes for expressjs. I read about some methods online but if I use them my app will become confusing, so can you suggest me a good blog, tutorial or video where I can learn to set up my app?

Basically I want to do some thing like this:

There will be one file which will be called every time: index.js. We will require one routes.js file in that, and in routes.js we will include all of our service files like login.js, signup.js etc., so that when any url encounters it then that particular code executes. The flow will be like:

  • index.js
  • routes.js
  • File where the URL matches.

I'm confused about this setup.

Using middleware you could do what you want , look at this example


var routes = require('routes')


module.exports = {
  user : require(./user),
  other : require(./other)


var router = require("express").Router()

router.get("/",function (req,res){
  // GET /user
.post("/",function (req,res){
  //POST /user

module.exports = router;


var router = require("express").Router()

router.get("/",function (req,res){
  // GET /other
.post("/",function (req,res){
  //POST /other

module.exports = router;

Build RESTful API using NodeJS, ExpressJS and MongoDB, We have successfully setup our express server. Although we defined the “/” route in our index.js file, we'll structure our project in a way that  These routing methods specify a callback function (sometimes called “handler functions”) called when the application receives a request to the specified route (endpoint) and HTTP method. In other words, the application “listens” for requests that match the specified route(s) and method(s), and when it detects a match, it calls the specified callback function.

That's actually quite simple.

First of all you need a file—let's call it app.js—in your main directory.

All the app.js needs to contain for a beginning is

var express = require('express');
var path = require('path');
var app = express();

// viewed at http://localhost:8080
app.get('/', function(req, res) {
    // send file to client
    res.sendFile(path.join(__dirname + '/index.html'));

// code gets executed when you fire a post request at http://localhost:8080/login'/login', function(req, res) {
    // execute code
    // verify user, authenticate, store user in session/token/cookie
    // return/send results/status
});'/signup', function(req, res) {
    // execute code
    // create user object, create database record, store user in session/token/cookie
    // return/send results/status


You can easily source the routes out, so your app.js doesn't become too heavy and your application is more structured, but the code above should cover your needs for a start.

Hope it helps!

Build a REST API with Node, Express & MongoDB!, Setting Up Our Routes. Now that our server file is all setup, let us now switch gears to our new subscribers.js routes file. To start,  A tutorial on how to setup MongoDB for Express.js in a Node.js application. It comes with the database installation and how to connect it to Express with Mongoose as ORM. You can choose to use another ORM, if you want to

use this to for scaffolding app.js routes everything to one routes defining file.


allRoutes.js has all route definitions with controllers definitions

var roleCtrl=require('../master/role');

My role.js looks like this

    const {ObjectId} = require('mongodb');

    module.exports =roleObj=

            collection: require(__basedir+'/config/config').clientName +'role',

            create: function (req, res) {

                var db =;

                var role =
                        roleName: req.body.roleName,
                        auditTrail :[ {timeStamp:null , userID:null, userName:null , change:null } ]


                    var response={_id:result.insertedId, message:"inserted", success:true};


            retrieve: function (req, res) {

                var db =;

                var input = {};

                if (req.params.roleId != undefined)
                   input['_id'] = ObjectId(req.params.roleId);

                db.collection(roleObj.collection).find(input).toArray(function (err, result) 

                    if (result.length != 0) res.send(result);

                    else res.send("empty");




Creating a REST API with Express.js and MongoDB, A Node.js with Express and MongoDB tutorial to learn step by step how to create a It gives you everything to expose an API (Express routes), to add Previously we have set up MongoDB in our Express.js application and  Express is one of the most popular web frameworks for Node.js that supports routing, middleware, view system… Mongoose is a promise-based Node.js ODM for MongoDB that provides a straight-forward, schema-based solution to model our application data along with built-in type casting, validation, query building, business logic hooks…

Expressjs routing setup with nodejs and mongodb, Using middleware you could do what you want , look at this example. app.js var routes = require('routes') app .use("/user",routes.user)  How To Build Simple RESTful API With NodeJs, ExpressJs And MongoDb. Our current setup requires that we restart the server each time we make changes to our files or add new ones. This can

Writing a CRUD app with Node.js and MongoDB, npm install --save express body-parser mongoose the dependencies we previously installed (ExpressJS and body-parser ) -we will talk about mongoose later- Last step before trying out our first route is to add the route class to the app.js Build RESTful API using NodeJS, ExpressJS and MongoDB A REST API is an integral component of a web application. You don’t need to be an expert backend developer to make your own.

Using Node.js & Express.js to save data to MongoDB Database, In this tutorial I will show you how to use Express.js, Node.js and MongoDB.js. This command will install the express module into our package.json. An endpoint is a route that our server will be listening to to get data from the browser. Part of a complete node.js series, including the usage of Express.js and much more! Time to use MongoDB! In this video we'll install it and you'll see the very basics of how to use it. Learn Node

  • Yes I have tried code like that and it worked, but now how can I source the routes out, can you please provide a simple example for that also ?