- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在使用 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)
以下是我为启动此引擎所做的设置。
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');
};
最佳答案
如果你不使用 View 引擎,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
});
关于node.js - 错误 : No default engine was specified and no extension was provided,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23595282/
在 CSS 中,我从来没有真正理解为什么会发生这种情况,但每当我为某物分配 margin-top:50% 时,该元素就会被推到页面底部,几乎完全消失这一页。我假设 50% 时,该元素将位于页面的中间位
我想在 MongoDB 中使用 Grails2.5 中的“ElasticSearch”插件。我的“BuildConfig.groovy”文件是: grails.servlet.version = "3
我有一个我想要处理的 OHLC 股票报价数组。 Open High Low Close Volume 2003-01-05
我尝试创建一个PreparedStatement: stmt = conn.prepareStatement("SELECT POLBRP, POLTYP, POLNOP, INCPTP, TRMTH
我的目录结构如下: root libA CMakeLists.txt ClassA.cpp libB CMakeLists.txt ClassB.cpp s
我是 DBMS 的新手。我在每个用户的不同 csv 文件中都有车辆痕迹。格式:名称,时间戳,纬度,经度,randomId。例如:user0,2008-10-2309:42:25,441972.6942
我需要为我的应用程序打上烙印,并且只需要自定义少量图像,代码库是相同的,只是生成的常量很少。 由于aapt 允许指定许多资源目录,有没有办法在Eclipse .classpath 文件中指定res 目
我希望在我的应用程序中实现 JWT,因为我正在通过引用以下内容对其进行一些研发:https://stormpath.com/blog/jwt-java-create-verify .当我尝试通过提取声
我正在尝试通过设置限制获取数据并根据时间戳对数据进行排序,但在运行应用程序时崩溃并显示此错误消息: 查询无效。在指定顺序之前不得指定起点。 我不知道为什么会这样。如何解决? 我需要数据序列和排序。
我正在使用Elasticsearch和Tire进行Rails3项目。当我尝试运行Elastic-search时,安装它后,出现以下错误: The stack size specified is too
我创建了一个简单的函数来执行 Http PUT 请求 - public string checkIfUserExists(string userName) { var endP
Java 安全管理器允许通过定义如下子句来指定某些代码段的权限: ... grant codebase http://foo.bar.com/test.jar { permission java
这更像是一种验证。 在 Oracle/Java 教程页面上,例如 this , 我一直看到catch 旁边的“specify”就好像它是另一个语句在异常处理中具有一些功能。 据我所知,“catch o
本文整理了Java中org.batfish.specifier.ZoneNameRegexInterfaceSpecifier类的一些代码示例,展示了ZoneNameRegexInterfaceSpe
我正在尝试运行以下命令: ionic cordova run android --device 但我收到以下错误 BUILD FAILED in 3s (node:3956) Unha
在不包含 viewport 元标记的网页上,大多数移动浏览器会将页面上的部分或全部字体“提升”到大于 css 指定的大小。例如,在移动版 Safari 上,7px 的指定大小将提升为类似 12px 的
嗨,我不了解 keras fit_generator 文档。 我希望我的困惑是理性的。 有一个batch_size还有分批训练的概念。使用 model_fit() ,我指定一个 batch_size
我使用 IProviderSearchContext 在 Sitecore 8.1(Lucene 搜索)中搜索特定项目,并获得每个项目的两个版本(en、ar)。我的问题是:我是否必须为每个查询指定:
Except in a declaration of a constructor, destructor, or conversion function, at least one defining-
使用 GooglePageSpeed 分析在线商店(Shopware)导致每个图像上出现许多“未指定到期时间”的线条。 我想知道是因为网络服务器 (nginx) 在所有图像的响应中添加了 Last-M
我是一名优秀的程序员,十分优秀!