尝试运行node.js应用程序时出现以下错误:
Not Found
404
Error: Not Found
at Layer.app.use.res.render.message [as handle] (C:\node\app\app.js:41:15)
at trim_prefix (C:\node\app\node_modules\express\lib\router\index.js:240:15)
at C:\node\app\node_modules\express\lib\router\index.js:208:9
at Function.proto.process_params (C:\node\app\node_modules\express\lib\router\index.js:269:12)
at next (C:\node\app\node_modules\express\lib\router\index.js:199:19)
at next (C:\node\app\node_modules\express\lib\router\index.js:176:38)
at C:\node\app\node_modules\express\lib\router\index.js:137:5
at C:\node\app\node_modules\express\lib\router\index.js:250:10
at next (C:\node\app\node_modules\express\lib\router\index.js:160:14)
at next (C:\node\app\node_modules\express\lib\router\index.js:176:38)
下面是我在app.js中使用的代码
var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/app');
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');
app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(function(req,res,next){
req.db = db;
next();
});
app.use('/', routes);
app.use('/users', users);
///var express = require('express');
///var router = express.Router();
/// 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;
你可以告诉我这个问题的根本原因以及如何解决这个问题。
以下是我的路由器:
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res) {
res.render('index', { title: 'Express' });
});
router.get('/helloworld',function(req,res)
{
res.render('helloworld',{title: 'World of node js' });
});
router.get('/userlist', function(req, res) {
var db = req.db;
var collection = db.get('usercollection');
collection.find({},{},function(e,docs){
res.render('userlist', {
"userlist" : docs
});
});
});
module.exports = router;
答案 0 :(得分:0)
如果数据库处理需要一些时间,则路由器将不存在!因此,您将需要等待数据库查询处理。
我在自己的搜索路由器中构建的自己的应用中拥有此功能。填充两个下拉列表的查询大约需要7到15秒,因此我要在索引页面上放置15秒钟之类的“应用初始化”之类的内容...