Search code examples
node.jsgitcompatibility

Running node with express 4.x.x? Correct flow?


A current dilemma I have is involved with my use of node whilst working in a local Github repository. I have a two part question so to speak, so I'll be direct:

  1. When I try to run my node.js application, I find out I need the express module. Upon using npm install express I find out something along the lines of "no method 'configure' available," and further diagnosis leads me to find that versions 4.x.x don't use this configure method. Therefore my quick (and probably ignorant) fix has been to just use node 3.x.x (npm install express@3.4.6) instead.

  2. Meanwhile, I found my way around to adding the node_modules directory to my .gitignore, therefore allowing me not to worry about including that in my pushes to the team repo via git.

Am I doing this process wrong? Is there a way to more simply run my app using the latest node version?

Thanks for your time.

UPDATE: Just to restate my concern - Is there a better way I can run my node app without having to install these modules using express, since I have to use an older version? Or is it something I'm not setting up correctly which is leading to the incompatibility with express 4.x.x.


Solution

  • Your issue here is that there are some breaking changes between express 3 and 4. In Express.js 3, you had to .configure() your application with all the middlewares you needed (json, cookies, etc.). This is no longer possible with Express 4 as this was due to dependencies to Connect module. Now Express and Connect middlewares are really separated and you only take what you need by requiring them like any other module.
    Hope this helps :

    Express.js 3 way with middlewares :

    var express = require('express')
    
    var app = express();
    
    app.configure(function () {
        app.use(express.bodyParser());
        app.use(express.methodOverride());
        app.use(express.json());
    });
    

    Express.js 4 way with middlewares :

    var express    = require('express')
    var bodyParser = require('body-parser')
    
    var app = express()
    
    app.use(bodyParser.json());
    app.use(require('method-override')());
    

    You can either install your module by using npm install module-name or by setting up a package.json containing all your dependencies at the root of your directory.