简体   繁体   中英

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"
  }

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"
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM