执行GET时,Express JS返回404,这比localhost / users(localhost / users / about)更复杂

时间:2017-10-25 19:18:46

标签: javascript node.js http express get

我已经安装了Express框架并使用了应用程序生成器来创建基本的应用程序结构。

我正在尝试使用由安装程序生成的单独路径文件users.js.

我在users.js中有这段代码:

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

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

router.get('/about', function(req, res, next) {
  res.send('respond with a resource');
});

module.exports = router;

app.js包含:

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

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

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

我在/ users(http://localhost/users)上进行了一次AJAX调用,但它确实有效 但是当我尝试http://localhost/users/about时,我收到404错误:

错误:未找到     在...... \ app.js:30:13     在Layer.handle [as handle_request](... \ node_modules \ express \ lib \ router \ layer.js:95:5)     在trim_prefix(... \ node_modules \ express \ lib \ router \ index.js:317:13)     在... \ node_modules \ express \ lib \ router \ index.js:284:7     在Function.process_params(... \ node_modules \ express \ lib \ router \ index.js:335:12)     在下一个(... \ node_modules \ express \ lib \ router \ index.js:275:10)     在... \ node_modules \ express \ lib \ router \ index.js:635:15     在下一个(... \ node_modules \ express \ lib \ router \ index.js:260:14)     在Function.handle(... \ node_modules \ express \ lib \ router \ index.js:174:3)     在路由器(... \ node_modules \ express \ lib \ router \ index.js:47:12)

我是框架的新手,只是想弄清楚什么是错的,谢谢。

0 个答案:

没有答案