我正在使用node.js和引擎设置超文本传输协议服务器。然而,我不断遇到的问题,我有很少的信息如何解决,我将不胜感激一些帮助解决这个问题。
Error: No default engine was specified and no extension was provided.
at new View (...\node_modules\express\lib\view.js:41:42)
at Function.app.render (...\node_modules\express\lib\application.js:484:12)
at ServerResponse.res.render (...\node_modules\express\lib\response.js:783:7)
at Layer.handle (...\app.js:123:7)
at trim_prefix (...\node_modules\express\lib\router\index.js:225:17)
at c (...\node_modules\express\lib\router\index.js:198:9)
at Function.proto.process_params (...\node_modules\express\lib\router\index.js:253:12)
at next (...\node_modules\express\lib\router\index.js:189:19)
at next (...\node_modules\express\lib\router\index.js:202:7)
at next (...\node_modules\express\lib\router\index.js:166:38)
下面是我启动这个引擎的设置。
var http = require('http');
var module = require("module")
var logger = require('morgan');
var express = require('express');
var app = module.exports = express();
var silent = 'test' == process.env.NODE_ENV;
var httpServer = http.createServer(app); // app middleware
app.enable('strict routing');
// app.all('*', function(req, res, next)/*** CORS support.*/
// {
// if (!req.get('Origin')) return next();// use "*" here to accept any origin
// res.set('Access-Control-Allow-Origin', 'http://localhost:3000');
// res.set('Access-Control-Allow-Methods', 'GET, POST');
// res.set('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
// res.set('Access-Control-Allow-Max-Age', 3600);
// if ('OPTIONS' == req.method) return res.send(200);
// next();
// });
app.set('views', __dirname + '/views'); // general config
app.set('view engine', 'html');
app.get('/404', function(req, res, next){
next();// trigger a 404 since no other middleware will match /404 after this one, and we're not responding here
});
app.get('/403', function(req, res, next){// trigger a 403 error
var err = new Error('not allowed!');
err.status = 403;
next(err);
});
app.get('/500', function(req, res, next){// trigger a generic (500) error
next(new Error('keyboard cat!'));
});
app.use(express.static(__dirname + '/public'));
//error handlers
app.use(logErrors);
app.use(clientErrorHandler);
app.use(errorHandler);
// middleware with an arity of 4 are considered error handling middleware. When you next(err)
// it will be passed through the defined middleware in order, but ONLY those with an arity of 4, ignoring regular middleware.
function clientErrorHandler(err, req, res, next) {
if (req.xhr) {// whatever you want here, feel free to populate properties on `err` to treat it differently in here.
res.send(err.status || 500, { error: err.message });
}
else
{ next(err);}
};
// create an error with .status. we can then use the property in our custom error handler (Connect repects this prop as well)
function error (status, msg) {
var err = new Error(msg);
err.status = status;
return err;
};
function logErrors (err, req, res, next) {
console.error(err.stack);
next(err);
};
function errorHandler (err, req, res, next) {
res.status(500);
res.render('error', { error: err });
};
// Error handlers
// Since this is the last non-error-handling middleware use()d, we assume 404, as nothing else responded.
// $ curl http://localhost:3000/notfound
// $ curl http://localhost:3000/notfound -H "Accept: application/json"
// $ curl http://localhost:3000/notfound -H "Accept: text/plain"
app.use(function(req, res, next){
res.status(404);
if (req.accepts('html')) {// respond with html page
res.render('404', { url: req.url });
return;
}
if (req.accepts('json')) {// respond with json
res.send({ error: 'Not found' });
return;
}
res.type('txt').send('Not found');// default to plain-text. send()
});
// error-handling middleware, take the same form as regular middleware, however they require an
// arity of 4, aka the signature (err, req, res, next).when connect has an error, it will invoke ONLY error-handling middleware.
// If we were to next() here any remaining non-error-handling middleware would then be executed, or if we next(err) to
// continue passing the error, only error-handling middleware would remain being executed, however here
// we simply respond with an error page.
app.use(function(err, req, res, next){
// we may use properties of the error object here and next(err) appropriately, or if we possibly recovered from the error, simply next().
res.status(err.status || 500);
res.render('500', { error: err });
});
if (!module.parent) {// assigning to exports will not modify module, must use module.exports
app.listen(3000);
silent || console.log('Express started on port 3000');
};
注释掉代码中的res.render
行,然后添加next(err)
取而代之。如果未使用视图引擎,则
res.render
工具将抛出错误。
抱歉,你也必须注释掉这一行:
app.set('view engine', 'html');
我的解决方案将导致不使用视图引擎。您不需要视图引擎,但如果这是目标,请尝试以下方法:
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
//swap jade for ejs etc
使用视图引擎时,还需要
res.render
行。大概是这样的:
// 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);
next(err);
res.render('error', {
message: err.message,
error: {}
});
});
您缺少视图引擎,例如使用jade:
换衣服
app.set('view engine', 'html');
具有
app.set('view engine', 'jade');
如果您想使用html友好的语法,请改用ejs
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
编辑
正如你们从视图中看到的。js Express视图模块
module.exports = View;
/**
* Initialize a new `View` with the given `name`.
*
* Options:
*
* - `defaultEngine` the default template engine name
* - `engines` template engine require() cache
* - `root` root path for view lookup
*
* @param {String} name
* @param {Object} options
* @api private
*/
function View(name, options) {
options = options || {};
this.name = name;
this.root = options.root;
var engines = options.engines;
this.defaultEngine = options.defaultEngine;
var ext = this.ext = extname(name);
if (!ext && !this.defaultEngine) throw new Error('No default engine was specified and no extension was provided.');
if (!ext) name += (ext = this.ext = ('.' != this.defaultEngine[0] ? '.' : '') + this.defaultEngine);
this.engine = engines[ext] || (engines[ext] = require(ext.slice(1)).__express);
this.path = this.lookup(name);
}
您必须安装了默认引擎
Express
通过程序搜索默认布局视图。模板
如下所示:
mkdir(path + '/views', function(){
switch (program.template) {
case 'ejs':
write(path + '/views/index.ejs', ejsIndex);
break;
case 'jade':
write(path + '/views/layout.jade', jadeLayout);
write(path + '/views/index.jade', jadeIndex);
break;
case 'jshtml':
write(path + '/views/layout.jshtml', jshtmlLayout);
write(path + '/views/index.jshtml', jshtmlIndex);
break;
case 'hjs':
write(path + '/views/index.hjs', hoganIndex);
break;
}
});
正如你可以在下面读到的:
program.template = 'jade';
if (program.ejs) program.template = 'ejs';
if (program.jshtml) program.template = 'jshtml';
if (program.hogan) program.template = 'hjs';
默认视图引擎是jade
如果不使用视图引擎,res.render内容将抛出错误。
如果您只想为json提供服务,请将代码中的res.render('error',{error: err});
行替换为:
res.json({ error: err })
PS:人们通常在返回的对象中也有消息:
res.status(err.status || 500);
res.json({
message: err.message,
error: err
});
您好,我正在尝试使用passportjs和jsonwebtoken进行android应用程序的身份验证,但当我尝试生成一个令牌作为用户登录并使用postman it响应错误测试api时: 未指定默认引擎,也未提供扩展。在函数的新视图中(C:\newApp\awesomeProject\node\u modules\express\lib\View.js:61:11)。在ServerResponse
我试图将onclick事件添加到svg元素中。 custom.svg
问题内容: 我正在尝试在html页面中显示配置的值和角值服务中的版本代码,但显示的不是作者的名字,这是html代码 这是指令… 这是在其中配置和值的服务部分 我在开发人员控制台中遇到的错误是 问题答案: 确保将这些模块( myApp.services 和 myApp.directives )作为主要应用程序模块的依赖项加载,如下所示: 塞子: http ://plnkr.co/edit/wxuFx
问题内容: 使用Dropzonejs上传文件后,我不知道如何获取JSONresponse。 我只有这个: 我认为如果不手动初始化dropzone是不可能的,所以我将其更改为: 哪个返回 如何初始化dropzone,以便添加诸如maxFiles,maxSize之类的选项并获取 JSON 响应? 问题答案: 将Dropzone附加到对象而没有以下任何情况时,不会提供URL: 告诉dropzone在何处
我使用jhipster版本7.6.0,这是我的CacheConfiguration类。 以下是redisson的依赖关系: 这是我的应用程序dev.yml: 一切都是由jHipster生成的,在安装redis 3.2.100版后,我遇到了以下异常: 顺便说一下,项目正在成功编译。 提前感谢您的帮助!
OpenCV错误:未指定的错误(该函数未实现。使用Windows、GTK 2.x或Carbon支持重建库。如果您在Ubuntu或Debian上,请安装libgtk2.0-dev和pkg config,然后重新运行cmake或configure脚本),位于cvShowImage的/io/OpenCV/modules/highgui/src/window文件中。cpp,第545行回溯(最后一次调用):