错误:找不到nodejs / expressjs中的app.use.res.render.message

时间:2015-09-25 06:51:30

标签: node.js express

我已经设置了新的express(nodejs)项目,当我尝试运行url时,它向我显示404找不到错误的错误。但我没有修改任何单行代码和目录结构。我使用了express-generator命令来创建快速项目的结构。

有人能为我提供以下问题的建议吗?   - 为什么默认代码不运行?   - 我应该如何设置我的项目目录以防止写这个长网址。   - 我如何将我的聊天应用程序与yii1项目集成。

以下是我的代码详情。

网址:http://localhost:3000/wchat/wnode/users

目录结构: wchat / wnode /用户 wchat:php项目 wnode:节点目录,用于实现即时通知和聊天 用户:模块

代码文件: app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = 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', 'jade');

// 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('/', routes);
app.use('/users', users);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user

app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {

   message: err.message,
   error: {}
  });
});
module.exports = app;

index.js

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

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

   module.exports = router;

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

module.exports = router;

错误:

Not Found
404
Error: Not Found
    at app.use.res.render.message (/var/www/html/wchat/wnode/app.js:30:13)
    at Layer.handle [as handle_request] (/var/www/html/wchat/wnode/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/var/www/html/wchat/wnode/node_modules/express/lib/router/index.js:312:13)
    at /var/www/html/wchat/wnode/node_modules/express/lib/router/index.js:280:7
    at Function.process_params (/var/www/html/wchat/wnode/node_modules/express/lib/router/index.js:330:12)
    at next (/var/www/html/wchat/wnode/node_modules/express/lib/router/index.js:271:10)
    at /var/www/html/wchat/wnode/node_modules/express/lib/router/index.js:618:15
    at next (/var/www/html/wchat/wnode/node_modules/express/lib/router/index.js:256:14)
    at Function.handle (/var/www/html/wchat/wnode/node_modules/express/lib/router/index.js:176:3)
    at router (/var/www/html/wchat/wnode/node_modules/express/lib/router/index.js:46:12)

1 个答案:

答案 0 :(得分:4)

看看你的路线,

app.use('/', routes); -> handlers GET on localhost:3000
app.use('/users', users); -> handles GET on localhost:3000/users

所以没有什么可以处理localhost:3000/wchat/wnode/users。因此404。

如果你想处理像localhost:3000/wchat/wnode/users这样的事情, 你需要一个像

这样的路线
router.verb('/wchat/wnode/users', function (req, res, next) {
  // handle whatever you want.
});

查看您的代码以及您尝试获取的网址,我建议您熟悉快速处理路由的方式。更多信息here