错误:未指定默认引擎且未提供扩展/控制器使用情况

时间:2015-06-14 19:37:27

标签: node.js controller

您好我试着开始使用MEAN。现在我得到这个错误:

Error: No default engine was specified and no extension was provided.
   at new View (C:\Users\twwaaaaa\nodeprojects\fahrtenbuch\node_modules\express\lib\view.js:48:42)
   at EventEmitter.app.render (C:\Users\twwaaaaa\nodeprojects\fahrtenbuch\node_modules\express\lib\application.js:545:12)
   at ServerResponse.res.render (C:\Users\twwaaaaa\nodeprojects\fahrtenbuch\node_modules\express\lib\response.js:938:7)
   at C:\Users\twwaaaaa\nodeprojects\fahrtenbuch\app.js:62:9
   at Layer.handle_error (C:\Users\twwaaaaa\nodeprojects\fahrtenbuch\node_modules\express\lib\router\layer.js:58:5)
   at trim_prefix (C:\Users\twwaaaaa\nodeprojects\fahrtenbuch\node_modules\express\lib\router\index.js:300:13)
   at C:\Users\twwaaaaa\nodeprojects\fahrtenbuch\node_modules\express\lib\router\index.js:270:7
   at Function.proto.process_params (C:\Users\twwaaaaa\nodeprojects\fahrtenbuch\node_modules\express\lib\router\index.js:321:12)
   at next (C:\Users\twwaaaaa\nodeprojects\fahrtenbuch\node_modules\express\lib\router\index.js:261:10)
   at Layer.handle_error (C:\Users\twwaaaaa\nodeprojects\fahrtenbuch\node_modules\express\lib\router\layer.js:60:5)
   at trim_prefix (C:\Users\twwaaaaa\nodeprojects\fahrtenbuch\node_modules\express\lib\router\index.js:300:13)
   at C:\Users\twwaaaaa\nodeprojects\fahrtenbuch\node_modules\express\lib\router\index.js:270:7
   at Function.proto.process_params (C:\Users\twwaaaaa\nodeprojects\fahrtenbuch\node_modules\express\lib\router\index.js:321:12)
   at next (C:\Users\twwaaaaa\nodeprojects\fahrtenbuch\node_modules\express\lib\router\index.js:261:10)
   at C:\Users\twwaaaaa\nodeprojects\fahrtenbuch\app.js:41:5
   at Layer.handle [as handle_request] (C:\Users\twwaaaaa\nodeprojects\fahrtenbuch\node_modules\express\lib\router\layer.js:82:5) 

它只是一个简单的应用程序,它应该将json数据添加到数据库中。

因此我想使用控制器。

fahrten.js

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

var fahrten = require('../controllers/fahrtenController');

/* POST /fahrten */
router.post('/', fahrten.post);

module.exports = router;

fahrtenController.js

require('../models/fahrt');
var mongoose = require('mongoose');

var Fahrt = mongoose.model("Fahrt");

exports.post = function(req, res) {
    var fahrt = new Fahrt(req.body);
    fahrt.save();
    res.jsonp(fahrt);
};

这样的事情很有效率

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

因此问题应该与控制器的处理有关吗?

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 fahrten = require('./routes/fahrten');

var app = express();

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/fahrtenbuch');
var db = mongoose.connection;

db.on('error', function callback(){
    console.log("Verbindung zu MongoDB fehlgeschlagen");
});

db.once('open', function callback(){
    console.log("Verbindung zu MongoDB erfolgreich");
});


app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: true
}));
app.use(cookieParser());

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

/// 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;

0 个答案:

没有答案