When to use next() and return next() in Node.js

As @Laurent Perrin’s answer: If you don’t do it, you risk triggering the callback a second time later, which usually has devastating results I give an example here if you write middleware like this: app.use((req, res, next) => { console.log(‘This is a middleware’) next() console.log(‘This is first-half middleware’) }) app.use((req, res, next) => { console.log(‘This … Read more

What does “./bin/www” do in Express 4.x?

In Express 3.0, you normally would use app.configure() (or app.use()) to set up the required middleware you need. Those middleware you specified are bundled together with Express 3.0. Example: var express = require(‘express’); var routes = require(‘./routes’); var user = require(‘./routes/user’); var http = require(‘http’); var path = require(‘path’); var app = express(); // all … Read more

How to properly reuse connection to Mongodb across NodeJs application and modules

You can create a mongoUtil.js module that has functions to both connect to mongo and return a mongo db instance: const MongoClient = require( ‘mongodb’ ).MongoClient; const url = “mongodb://localhost:27017”; var _db; module.exports = { connectToServer: function( callback ) { MongoClient.connect( url, { useNewUrlParser: true }, function( err, client ) { _db = client.db(‘test_db’); return … Read more

Failed to load c++ bson extension

I guess you did not have the make tools available when you installed your mongodb library. I suggest you do xcode-select –install (on a mac) or sudo apt-get install gcc make build-essential (on ubuntu) and run rm -rf node_modules npm cache clean npm install OR just npm update based on @tobias comment (after installing build-essential) … Read more