快速路由:404 在某些路由名称上,但在其他路由名称上不是?
Express Routing: 404 on some route names, but not others?
我正在制作一个前端带有 Angular 路由的单页应用程序,但后端让我很伤心。
所有涉及与数据库接口的路由都有效,但是当我添加路由(复制完全相同的结构)以发送 html 文件或只是 hello world 时,它 returns 一个 404,甚至没有在节点控制台上注册(下面提到了一个奇怪的例外)。
我阅读了 this post 解释 static
应该如何出现在 routes
之后,这很有道理,但它不适用于我的代码。
app.js如下:
// SETUP
var express = require('express');
var app = express();
var router = express.Router();
var mongoose = require('mongoose');
var port = process.env.PORT || 3001;
var database = 'mongodb://example-database...';
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
// CONFIG
mongoose.connect(database);
// Note: response parser must be placed before routing
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.set('jsonp callback name', 'cb');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(router);
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
next();
});
// ROUTES
require("./routes/api_chirp.js")(router);
require("./routes/api_video.js")(router);
require("./routes/api_interaction.js")(router);
require("./routes/api_user.js")(router);
require("./routes/api_search.js")(router);
require("./routes/api_jsonp.js")(router);
require("./routes/index.js")(router);
app.use(express.static(path.join(__dirname, 'public')));
// ERROR HANDLING
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
module.exports = app;
app.listen(port);
console.log("App listening on port " + port);
index.js(注意:在其他工作 API 路线中使用的相同结构(使用替代路线,即“/api/someroute” )
module.exports = function(router) {
router.route('/workspace') // Only works if route is prefixed with "api_someword", "someword" itself won't work.
.get(function(req, res) {
console.log('request received');
res.send('hello world');
}); // Ends - Post
};
问题:我已经无路可退了...此时任何建议和答案都会有所帮助。我如何让 index.js 中的那条路线正常工作?
// ROUTES
require("./routes/api_chirp.js")(router);
require("./routes/api_video.js")(router);
require("./routes/api_interaction.js")(router);
require("./routes/api_user.js")(router);
require("./routes/api_search.js")(router);
require("./routes/api_jsonp.js")(router);
require("./routes/index.js")(router);
像这样传递应用程序对象
// 路线
require("./routes/api_chirp.js")(app);
require("./routes/api_video.js")(app);
require("./routes/api_interaction.js")(app);
require("./routes/api_user.js")(app);
require("./routes/api_search.js")(app);
require("./routes/api_jsonp.js")(app);
require("./routes/index.js")(app);
已解决:
另一位开发人员在 nginx 上放置了一个路径过滤器,以阻止任何 url 没有前缀 /api
的东西到达节点。我添加了它,它解决了问题。
我正在制作一个前端带有 Angular 路由的单页应用程序,但后端让我很伤心。
所有涉及与数据库接口的路由都有效,但是当我添加路由(复制完全相同的结构)以发送 html 文件或只是 hello world 时,它 returns 一个 404,甚至没有在节点控制台上注册(下面提到了一个奇怪的例外)。
我阅读了 this post 解释 static
应该如何出现在 routes
之后,这很有道理,但它不适用于我的代码。
app.js如下:
// SETUP
var express = require('express');
var app = express();
var router = express.Router();
var mongoose = require('mongoose');
var port = process.env.PORT || 3001;
var database = 'mongodb://example-database...';
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
// CONFIG
mongoose.connect(database);
// Note: response parser must be placed before routing
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.set('jsonp callback name', 'cb');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(router);
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
next();
});
// ROUTES
require("./routes/api_chirp.js")(router);
require("./routes/api_video.js")(router);
require("./routes/api_interaction.js")(router);
require("./routes/api_user.js")(router);
require("./routes/api_search.js")(router);
require("./routes/api_jsonp.js")(router);
require("./routes/index.js")(router);
app.use(express.static(path.join(__dirname, 'public')));
// ERROR HANDLING
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
module.exports = app;
app.listen(port);
console.log("App listening on port " + port);
index.js(注意:在其他工作 API 路线中使用的相同结构(使用替代路线,即“/api/someroute” )
module.exports = function(router) {
router.route('/workspace') // Only works if route is prefixed with "api_someword", "someword" itself won't work.
.get(function(req, res) {
console.log('request received');
res.send('hello world');
}); // Ends - Post
};
问题:我已经无路可退了...此时任何建议和答案都会有所帮助。我如何让 index.js 中的那条路线正常工作?
// ROUTES
require("./routes/api_chirp.js")(router);
require("./routes/api_video.js")(router);
require("./routes/api_interaction.js")(router);
require("./routes/api_user.js")(router);
require("./routes/api_search.js")(router);
require("./routes/api_jsonp.js")(router);
require("./routes/index.js")(router);
像这样传递应用程序对象
// 路线
require("./routes/api_chirp.js")(app);
require("./routes/api_video.js")(app);
require("./routes/api_interaction.js")(app);
require("./routes/api_user.js")(app);
require("./routes/api_search.js")(app);
require("./routes/api_jsonp.js")(app);
require("./routes/index.js")(app);
已解决:
另一位开发人员在 nginx 上放置了一个路径过滤器,以阻止任何 url 没有前缀 /api
的东西到达节点。我添加了它,它解决了问题。