Kob*_*kie 116 http node.js express
我正在使用node.js和引擎设置http服务器.但是,我一直在遇到一些问题,我几乎没有关于如何解决的信息我会很感激帮助解决这个问题.提前致谢.
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)
Run Code Online (Sandbox Code Playgroud)
以下是我为启动此引擎而设置的内容.
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');
};
Run Code Online (Sandbox Code Playgroud)
Pyl*_*nux 101
如果你没有使用视图引擎,res.render的东西会抛出一个错误.
如果您只想提供json res.render('error', { error: err });
,请使用以下代码替换代码中的行:
res.json({ error: err })
Run Code Online (Sandbox Code Playgroud)
PS:人们通常在返回的对象中也有消息:
res.status(err.status || 500);
res.json({
message: err.message,
error: err
});
Run Code Online (Sandbox Code Playgroud)
ale*_*dro 91
您缺少视图引擎,例如使用jade:
改变你的
app.set('view engine', 'html');
Run Code Online (Sandbox Code Playgroud)
同
app.set('view engine', 'jade');
Run Code Online (Sandbox Code Playgroud)
如果你想使用html友好语法,请使用ejs
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
Run Code Online (Sandbox Code Playgroud)
编辑
您可以从view.js Express View Module中读取
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);
}
Run Code Online (Sandbox Code Playgroud)
你必须安装了一个 default engine
Express
搜索默认布局视图,program.template
如下所示:
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;
}
});
Run Code Online (Sandbox Code Playgroud)
你可以在下面看到:
program.template = 'jade';
if (program.ejs) program.template = 'ejs';
if (program.jshtml) program.template = 'jshtml';
if (program.hogan) program.template = 'hjs';
Run Code Online (Sandbox Code Playgroud)
默认视图引擎是 jade
cao*_*key 14
注释掉res.render
代码中的行并添加next(err);
.如果你没有使用视图引擎,这些res.render
东西会抛出错误.
对不起,您还必须注释掉这一行:
app.set('view engine', 'html');
Run Code Online (Sandbox Code Playgroud)
我的解决方案会导致不使用视图引擎.您不需要视图引擎,但如果这是目标,请尝试以下操作:
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
//swap jade for ejs etc
Run Code Online (Sandbox Code Playgroud)
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: {}
});
});
Run Code Online (Sandbox Code Playgroud)
Niv*_*ina 10
如果你想渲染一个html文件使用
response.sendfile('index.html');
Run Code Online (Sandbox Code Playgroud)
然后你删除了
app.set('view engine', 'html');
Run Code Online (Sandbox Code Playgroud)
唯一的事情是将index.html放在视图中.或将公用文件夹设为静态文件夹并将index.html公开
Rah*_*din 10
代替
app.get('/', (req, res) => res.render('Hellooooo'))
Run Code Online (Sandbox Code Playgroud)
使用
app.get('/', (req, res) => res.send('Hellooooo'))
Run Code Online (Sandbox Code Playgroud)
小智 7
请更换
app.set('view engine', 'html');
Run Code Online (Sandbox Code Playgroud)
和
app.set('view engine', 'ejs');
Run Code Online (Sandbox Code Playgroud)
设置视图引擎以下方式
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
186531 次 |
最近记录: |