0
我正在嘗試爲我的站點的一個子集構建一個catch-all路由,其中包含需要由ejs處理的多個目錄/頁面,但不需要特定的路線。res.render在調用fs.stat/fs.access之後導致本地err
[NB,我包括在下面的例子中,同步版本,以除去在相同的行爲的結果的定時的任何問題...的類似實施fs.stat的(和fs.access的)]
router.get('/foobar/:page(*)', function(req, res, next) {
var fs = require('fs');
var path = require('path');
var viewsPath = req.app.get('views');
try
{
// see if there's an ejs file corresponding to request
var stats = fs.statSync(path.join(viewsPath, 'foobar', req.params.page + '.ejs'));
console.log(stats); // stats object seems accurate
// THROWS WHEN CALLING openSync
res.render('foobar/' + req.params.page, {title: 'foo'});
}
catch (e)
{
var err = new Error();
err.status = 404;
next(err);
}
}
404路工作得很好,但是當我要求實際存在的網頁,渲染調用拋出:
Unexpected error code undefined has occurred. Please retry your request
at Error (native)
at Object.fs.openSync (fs.js:500:18)
at Object.fs.readFileSync (fs.js:352:15)
at includeSource (C:\Users\Jim\Documents\myProject\node_modules\ejs\lib\ejs.js:194:17)
at C:\Users\Jim\Documents\myProject\node_modules\ejs\lib\ejs.js:528:26
at Array.forEach (native)
at Object.Template.generateSource (C:\Users\Jim\Documents\myProject\node_modules\ejs\lib\ejs.js:505:15)
at Object.Template.compile (C:\Users\Jim\Documents\myProject\node_modules\ejs\lib\ejs.js:427:12)
at Object.compile (C:\Users\Jim\Documents\myProject\node_modules\ejs\lib\ejs.js:288:16)
at handleCache (C:\Users\Jim\Documents\myProject\node_modules\ejs\lib\ejs.js:147:16)
at View.exports.renderFile [as engine] (C:\Users\Jim\Documents\myProject\node_modules\ejs\lib\ejs.js:350:14)
at View.render (C:\Users\Jim\Documents\myProject\node_modules\express\lib\view.js:126:8)
at tryRender (C:\Users\Jim\Documents\myProject\node_modules\express\lib\application.js:639:10)
at EventEmitter.render (C:\Users\Jim\Documents\myProject\node_modules\express\lib\application.js:591:3)
at ServerResponse.render (C:\Users\Jim\Documents\myProject\node_modules\express\lib\response.js:961:7)
at C:\Users\Jim\Documents\myProject\routes\power-essentials.js:32:7
at Layer.handle [as handle_request] (C:\Users\Jim\Documents\myProject\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Jim\Documents\myProject\node_modules\express\lib\router\route.js:131:13)
at Route.dispatch (C:\Users\Jim\Documents\myProject\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Jim\Documents\myProject\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Jim\Documents\myProject\node_modules\express\lib\router\index.js:277:22
at param (C:\Users\Jim\Documents\myProject\node_modules\express\lib\router\index.js:349:14)
它的行爲幾乎像fs.statSync呼叫已放置在文件的鎖在t時呈現扼流圈試圖打開視圖文件。