app.post is not a function express node

核能气质少年 提交于 2019-12-10 17:56:39

问题


I had all my routes in server.js but I wanted to make it modular and put into a folder called routes. I created a file called apis.js in routes folder but as I did that I get TypeError: app.post is not a function

server.js:

var express    = require('express');
var app        = express();

var bodyParser = require('body-parser');
var urlencode = bodyParser.urlencoded({ extended: true});

app.use(express.static('public'));

var apis = require('./routes/apis');
app.use('/', apis);

module.exports = app;

apis.js:

  module.exports = function(app){

  app.get('/', function(req, res) {
    res.send('OK');
  });

  app.post('/idea', function(req, res) {
  ...
  });

};

Also, having module.exports = app in server.js is important as I have tests running and I want a instance of app everytime.

What am I missing?


回答1:


Better approach :-

server.js

var express    = require('express');
var app        = express();

var bodyParser = require('body-parser');
var urlencode = bodyParser.urlencoded({ extended: true});

app.use(express.static('public'));

var apis = require('./routes/apis');
app.use('/', apis);

module.exports = app;

apis.js :-

var router = require('express').Router();
router.post('/url',function(req,res,next){
 //your code 
})

module.exports = router



回答2:


You need to pass in your express app into your apis module so it can attach the routes to your app. If you want to use app.use to put your routes in a different root path, you can create another express router, and attach your routes to that, then app.use that router:

server.js:

var express    = require('express');
var app        = express();

var bodyParser = require('body-parser');
var urlencode = bodyParser.urlencoded({ extended: true});

app.use(express.static('public'));
var apis = express.Router();

require('./routes/apis')(apis);

app.use('/', apis);
module.exports = app;



回答3:


There's a couple different methods for connecting your app to your routes, and it looks to me like you are mixing them together, resulting in your error.

The fix already mentioned...

var router = require('express').Router();
router.post('/url',function(req,res,next){
 //your code 
})

module.exports = router

...works with the way you currently have your server.js file set up.

Another method that doesn't require you to modify your apis.js file is to require it in server.js using

require("./routes/apis.js")(app);

instead of

var apis = require('./routes/apis');
app.use('/', apis);

This ensures that the variable app is passed into the function in apis.js

The first version is recommended, but hopefully this explains why you are getting confused between the two, i.e. because the second is an alternate version.

See Differences between express.Router and app.get? for more information on why the router version is recommended.



来源:https://stackoverflow.com/questions/38758198/app-post-is-not-a-function-express-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!