快速路由:某些路由名称为404,但其他路由名称不是?

时间:2015-02-26 02:56:21

标签: javascript angularjs node.js express url-routing

我正在制作一个单页应用程序,前端有Angular路由,但后端让我感到悲伤。

涉及与数据库接口的所有路由都有效,但是当我添加路由(复制完全相同的结构)以发送html文件或只是hello world时,它返回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中使用该路由?

2 个答案:

答案 0 :(得分:0)

// 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对象

// ROUTES

  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);

答案 1 :(得分:0)

解决:

另一位开发人员在nginx上设置了一个路径过滤器,阻止了任何不具有/api前缀的网址以阻止其进入节点。我添加了它,它解决了这个问题。