我遇到了麻烦,已经花了很长时间才弄明白原因但无济于事。我已经研究过并觉得我做得对,但显然我错过了一些东西。
这是我的app.js:
var express = require('express');
var app = express();
var port = process.env.PORT || 8080;
var bodyParser = require('body-parser');
var session = require('express-session');
app.use(express.static('./server/static/'));
app.use(express.static('./client/dist/'));
// tell the app to parse HTTP body messages
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser()); // read cookies (needed for auth)
//routes
var authRoutes = require('./server/routes/auth');
app.use('/auth', authRoutes);
app.listen(3000, () => {
console.log('Server is running on http://localhost:3000);
});
这是单独的auth(路由文件):
var express = require('express');
var router = express.Router();
router.post('/signup', (req, res, next) => {
console.log("im in");
});
module.exports = router;
运行之后,我得到404响应: POST http://localhost:3000/signup 404(未找到)
如果我将路线放在app.js文件中,我会得到所需的输出。有人可以帮我弄清楚我做错了什么吗?
答案 0 :(得分:2)
尝试http://localhost:3000/auth/signup
app.use('/auth', authRoutes);
在以authRoutes
开头的路径上公开/auth
。