3

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"
  }
dx_over_dt
  • 13,240
  • 17
  • 54
  • 102
  • 3
    `express.Router` was [introduced with Express 4.0](https://github.com/strongloop/express/wiki/New-features-in-4.x) (vs. `^3.4.4`). If the function is available at all with Express 3.x, it's an extension being defined by another module. – Jonathan Lonowski Apr 06 '15 at 03:56
  • 1
    I think you are using too old version of express. You try with express 4.x. or just mention `*` and re install the npm package in your local nom_modules package. – NarendraSoni Apr 06 '15 at 03:58
  • Yup, I updated my packages to the current version, and revised my app.js. I'll have to do some more work making it functional, but `.Router()` works now. Thanks! – dx_over_dt Apr 06 '15 at 04:06
  • @JonathanLonowski You may want to add that as an answer so that the OP can give you the correct answer. – JoeMoe1984 Apr 08 '15 at 16:51

2 Answers2

4

Upgrading your Express JS package may help

npm install express@4.13.4 -g

OR

npm install express@4.13.4 --save-dev

OR

//Edit your pakage.json
"dependencies": {
    "express": "~4.13.4"
}
digender mahara
  • 467
  • 3
  • 15
0

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 express@4.13.4 --save

davidrl1000
  • 311
  • 1
  • 4
  • 16