0

我在 EC2 上创建了一个 ubuntu 服务器实例,并试图让快速生成器简单骨架开始工作。
当我转到根http://MY_SERVER_IP_HERE/时,会显示应用程序的索引页面。
如果我去http://MY_SERVER_IP_HERE/users我得到 404,即使设置了快速应用程序来处理这条路线。

var routes = require('./routes/index');
var users = require('./routes/users');

app.use('/', routes);
app.use('/users', users);

/routes/index.js

var express = require('express');
var router = express.Router();

/* GET home page. */
/**** WORKS FINE ****/
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

module.exports = router;

/routes/users.js

var express = require('express');
var router = express.Router();

/* GET users listing. */
/*** GETTING 404 ****/
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

module.exports = router;

我的 nginx 路由设置:

location ~ ^/(stylesheets/|css/|js/){
    root /var/www/html/test/myapp/public;
}

location / {
    proxy_pass http://MY_SERVER_IP:3000;
}

非常感谢!

4

1 回答 1

0

像这样更改代码。

服务器.js

var express= require('express');
var app = express();  
require('./routes/index')(app, express);
require('./routes/users')(app, express);
//.....
app.listen(3000);

/routes/index.js

module.exports  = function(app, express) {
    var router = express.Router();
    router.get('/', function(req, res, next) {
        res.render('index', { title: 'Express' });
    });
    app.use('/', router);
};

/routes/users.js

module.exports  = function(app, express) {
    var router = express.Router();
    router.get('/', function(req, res, next) {
        res.send('respond with a resource');
    });
    app.use('/users', router);
};
于 2015-09-13T12:15:39.887 回答