我遇到了Nodejs的问题,导航到我的localhost:3000 / survey时收到错误信息。我遵循了一个广泛的教程,尽管通过stackoverflow搜索答案,我无法确定错误的原因,根据教程,一切都应该正常工作。当我导航到localhost:3000 / users时,它工作得非常好。我的路由文件users.js根据需要使用资源进行响应,我只是无法使localhost:3000 / survey工作。下面我发布了错误代码和我的调查的控制器,路线和模型。
我收到错误
Error: No default engine was specified and no extension was provided.
at new View (/Users/testuser/WebstormProjects/test/node_modules/express/lib/view.js:62:11)
at EventEmitter.render (/Users/testuser/WebstormProjects/test/node_modules/express/lib/application.js:569:12)
at ServerResponse.render (/Users/testuser/WebstormProjects/test/node_modules/express/lib/response.js:961:7)
at /Users/testuser/WebstormProjects/test/app.js:60:7
at Layer.handle_error (/Users/testuser/WebstormProjects/test/node_modules/express/lib/router/layer.js:71:5)
at trim_prefix (/Users/testuser/WebstormProjects/test/node_modules/express/lib/router/index.js:310:13)
at /Users/testuser/WebstormProjects/test/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/Users/testuser/WebstormProjects/test/node_modules/express/lib/router/index.js:330:12)
at next (/Users/testuser/WebstormProjects/test/node_modules/express/lib/router/index.js:271:10)
at Layer.handle_error (/Users/testuser/WebstormProjects/test/node_modules/express/lib/router/layer.js:73:5)
at trim_prefix (/Users/testuser/WebstormProjects/test/node_modules/express/lib/router/index.js:310:13)
at /Users/testuser/WebstormProjects/test/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/Users/testuser/WebstormProjects/test/node_modules/express/lib/router/index.js:330:12)
at next (/Users/testuser/WebstormProjects/test/node_modules/express/lib/router/index.js:271:10)
at /Users/testuser/WebstormProjects/test/app.js:39:3
at Layer.handle [as handle_request] (/Users/testuser/WebstormProjects/test/node_modules/express/lib/router/layer.js:95:5)
模型
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var SurveySchema = new Schema ({
created: {
type: Date,
default: Date.now
},
von: Date,
bis: Date,
kmStart: Number,
kmEnde: Number,
strecke: String,
zweck: String,
privat: Boolean,
fahrer: String,
fahrzeug: String
});
SurveySchema.statics = {
load: function(id, cb){
this.findOne({_id : id}).exec(cb);
}
};
mongoose.model('Survey', SurveySchema);
控制器
require('../models/survey');
var mongoose = require('mongoose');
var Survey = mongoose.model("Survey");
exports.post = function(req, res){
var survey = new Survey(req.body);
survey.save();
res.jsonp(survey);
}
路线
var express = require('express');
var router = express.Router();
var surveys = require('../controllers/surveysController');
/* POST /surveys */
router.post('/', surveys.post);
module.exports = router;
答案 0 :(得分:1)
@all我只是想说我确实解决了这个问题。我想是因为我在app.js文件中使用了错误的MongoDB。这一变化解决了这个问题。
mongoose.connect('mongodb://localhost/mydb');
要
mongoose.connect('mongodb://localhost/test');
答案 1 :(得分:0)
你能展示你的app.js吗?让我们确保你告诉应用程序使用路由器进行/调查
答案 2 :(得分:0)
这是app.js的要求
var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var users = require('./routes/users');
var surveys = require('./routes/surveys');
var app = express();
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mydb');
var db = mongoose.connection;
db.on('error', function callback(){
console.log("connection to MongoDB failed");
});
db.once('open', function callback(){
console.log("connection to MongoDB success");
});
// 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('/users', users);
app.use('/surveys', surveys);
// 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;
答案 3 :(得分:0)
我想我可能知道这个问题,但是如果它不起作用我会调整我的答案。 在您的代码中,我没有看到设置模板引擎的任何地方。你可以通过安装像Jade或Swig这样的东西来做到这一点。
npm install jade --save
然后告诉快递它就在那里。
app.set('view engine', 'jade');
然后,您可以设置将在模板中呈现的变量。
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message, //message will be rendered
error: {}
});
});
然后是模板;使用Jade,它会将代码预处理为实际的html。因此,邮件将呈现给<h1>This is an error message</h1>
html
head
body
h1= message
当然你可以写出HTML;这只是为了方便。以下是指向Jade或Swig的链接。如果您使用Swig,我可以使用两个左右花括号在页面中呈现数据。例如,
<div class="alert alert-danger" role="alert">
Error: {{message}}
</div>