我刚接触使用Express和Mongoos编写API,但我认为应该有用的东西让我感到悲伤。
我正在尝试尽可能模块化的东西,所以我有我的主app.js,一个model.js然后我的routes.js
这是我的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 mongoose = require('mongoose');
var cors = require('cors');
mongoose.connect('mongodb://localhost/guestbook');
var app = express();
// 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: true }));
app.use(cookieParser());
app.use(cors());
app.use('/api', require('./routes/api'));
// changes it to use the optimized version for production
app.use(express.static(path.join(__dirname, '/dist')));
// 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;
这是我在/ api(路线文件)中的内容
//DEPENDENCIES
var express = require('express');
var router = express.Router();
//GUESTBOOK END POINTS
var Guestbook = require('../models/guestbook')
router.route('/guestbook')
.post(function(req, res){
var guestbook = new Guestbook();
guestbook.firstname = req.body.firstname;
guestbook.lastname = req.body.lastname;
guestbook.email = req.body.email;
guestbook.postedon = req.body.postedon;
guestbook.comment = req.body.comment;
guestbook.rate = req.body.rate;
guestbook.save(function(err){
if(err)
res.send(err);
res.json({message:'Post created!'})
});
})
.get(function(req, res){
guestbook.find(function(err, guestbook){
if(err)
res.send(err);
res.json(guestbook);
});
});
//RETURN ROUTER AS MODULE
module.exports = router;
最后,这是我的模特:
//DEPENDENCIES
var restful = require('node-restful');
var mongoose = restful.mongoose;
var guestbookSchema = new mongoose.Schema({
firstname: String,
lastname: String,
email: String,
postedon: Date,
comment: String,
rate: Number
});
//RETURN MODEL
module.exports = mongoose.model('guestbook', guestbookSchema);
同样,帖子有效,但得不到。
这是我获得的console.log:
Error: No default engine was specified and no extension was provided.
at new View (/Users/christophernakea/Documents/Projects/TestAPIExpress/server/node_modules/express/lib/view.js:62:11)
at EventEmitter.render (/Users/christophernakea/Documents/Projects/TestAPIExpress/server/node_modules/express/lib/application.js:569:12)
at ServerResponse.render (/Users/christophernakea/Documents/Projects/TestAPIExpress/server/node_modules/express/lib/response.js:961:7)
at /Users/christophernakea/Documents/Projects/TestAPIExpress/server/app.js:32:13
at Layer.handle_error (/Users/christophernakea/Documents/Projects/TestAPIExpress/server/node_modules/express/lib/router/layer.js:71:5)
at trim_prefix (/Users/christophernakea/Documents/Projects/TestAPIExpress/server/node_modules/express/lib/router/index.js:310:13)
at /Users/christophernakea/Documents/Projects/TestAPIExpress/server/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/Users/christophernakea/Documents/Projects/TestAPIExpress/server/node_modules/express/lib/router/index.js:330:12)
at next (/Users/christophernakea/Documents/Projects/TestAPIExpress/server/node_modules/express/lib/router/index.js:271:10)
at Layer.handle_error (/Users/christophernakea/Documents/Projects/TestAPIExpress/server/node_modules/express/lib/router/layer.js:67:12)
at trim_prefix (/Users/christophernakea/Documents/Projects/TestAPIExpress/server/node_modules/express/lib/router/index.js:310:13)
at /Users/christophernakea/Documents/Projects/TestAPIExpress/server/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/Users/christophernakea/Documents/Projects/TestAPIExpress/server/node_modules/express/lib/router/index.js:330:12)
at Immediate.next (/Users/christophernakea/Documents/Projects/TestAPIExpress/server/node_modules/express/lib/router/index.js:271:10)
at Immediate.<anonymous> (/Users/christophernakea/Documents/Projects/TestAPIExpress/server/node_modules/express/lib/router/index.js:618:15)
at Immediate.immediate._onImmediate (timers.js:435:18)
答案 0 :(得分:0)
错误告诉您没有使用express框架设置模板引擎。请参阅与此主题相关的express guide。
这一行
res.render('error', {
message: err.message,
error: {}
});
呈现模板。但是你没有配置使用哪一个。
答案 1 :(得分:0)
问题是你试图在不指定模板引擎的情况下渲染页面,因为你只创建了一个API,你不需要渲染任何页面,所以你需要只返回一个json res.render(&#39;错误&#39;,{ 消息:err.message, 错误:{}
你可以用res.json替换它(&#39;错误&#39;:err.message)