dx_over_dt
dx_over_dt

Reputation: 14328

express.Router() is returning undefined

I'm trying to set up a rest API that runs on my node.js HTTP server. For regular calls, I want the path to be /..., for API calls, I want to use /API/....

From what I can gather from various webpages, including http://expressjs.com/guide/routing.html, what I need is something like this:

var express = require('express');
var app = express();
var router = express.Router();

/* set up app ... */

router.route('/some/url').get(...).put(...);
app.use('/API', router);

For some reason, however, the third line (var router = ...) always returns undefined. When I debug, I can plainly see that express.Router() is a function with no return statement, but does include a bunch of this.foo setters. This makes me think that I should be calling var router = new express.Router(), but I can't find any documentation to support that claim.

Ideas on what's going wrong?

My project dependencies in my packages.json files are:

  "dependencies": {
    "body-parser": "^1.12.2",
    "express": "^3.4.4",
    "express-react-views": "^0.7.1",
    "fluxxor": "^1.5.2",
    "mongoose": "^4.0.1",
    "react": "^0.12.2"
  }

Upvotes: 3

Views: 8524

Answers (2)

davidrl1000
davidrl1000

Reputation: 361

You must update or migrate your express to 4.#.# . Defenitly you are using a older version, versions 3.x and 2,x are deprecated. Here is a link for the migration. Also you can use this comands:

to get the last version

npm install express --save

or

to get the version 4.13.4

npm install [email protected] --save

Upvotes: 0

digender mahara
digender mahara

Reputation: 487

Upgrading your Express JS package may help

npm install [email protected] -g

OR

npm install [email protected] --save-dev

OR

//Edit your pakage.json
"dependencies": {
    "express": "~4.13.4"
}

Upvotes: 4

Related Questions