在 Express 应用程序中异步执行 res.render
Executing res.render asynchronously in express app
我尝试让这个路由器响应异步:
var express = require('express'),
router = express.Router();
router.get('/', function(req, res, next) {
res.render('contact', {
titleShown: true,
title: 'Contact'
});
});
我试图实现我读到的关于 here 的 async
,但没有成功:
var express = require('express'),
router = express.Router(),
async = require('async');
router.get('/', function(req, res, next) {
async.parallel([
res.render('contact', {
titleShown: true,
title: 'Contact'
})
], req);
});
我该怎么做?
我在使用 --trace-sync-io
标志时收到的错误消息:
WARNING: Detected use of sync API
at fs.statSync (fs.js:892:18)
at tryStat (C:\www\node\website\node_modules\express\lib\view.js:169:15)
at resolve (C:\www\node\website\node_modules\express\lib\view.js:142:14)
at lookup (C:\www\node\website\node_modules\express\lib\view.js:110:17)
at View (C:\www\node\website\node_modules\express\lib\view.js:85:20)
at render (C:\www\node\website\node_modules\express\lib\application.js:569:12)
at render (C:\www\node\website\node_modules\express\lib\response.js:961:7)
at C:\www\node\website\routes\contact.js:9:7
at handle (C:\www\node\website\node_modules\express\lib\router\layer.js:95:5)
不,res.render
不是完全异步的(目前)。所以错误真的来自 res.render
:
Yes, there are sync parts of the res.render API (which sucks), but it
will be addressed in Express 5.0, as we cannot address it without
breaking the view engine compatibility.
Starting your application with NODE_ENV=production or setting the
cache to true for rendering will cause file system activities only
once per view at startup, which makes this a non-issue while the
application is fully running in production, since no sync file systems
are called since the views are cached.
我尝试让这个路由器响应异步:
var express = require('express'),
router = express.Router();
router.get('/', function(req, res, next) {
res.render('contact', {
titleShown: true,
title: 'Contact'
});
});
我试图实现我读到的关于 here 的 async
,但没有成功:
var express = require('express'),
router = express.Router(),
async = require('async');
router.get('/', function(req, res, next) {
async.parallel([
res.render('contact', {
titleShown: true,
title: 'Contact'
})
], req);
});
我该怎么做?
我在使用 --trace-sync-io
标志时收到的错误消息:
WARNING: Detected use of sync API
at fs.statSync (fs.js:892:18)
at tryStat (C:\www\node\website\node_modules\express\lib\view.js:169:15)
at resolve (C:\www\node\website\node_modules\express\lib\view.js:142:14)
at lookup (C:\www\node\website\node_modules\express\lib\view.js:110:17)
at View (C:\www\node\website\node_modules\express\lib\view.js:85:20)
at render (C:\www\node\website\node_modules\express\lib\application.js:569:12)
at render (C:\www\node\website\node_modules\express\lib\response.js:961:7)
at C:\www\node\website\routes\contact.js:9:7
at handle (C:\www\node\website\node_modules\express\lib\router\layer.js:95:5)
不,res.render
不是完全异步的(目前)。所以错误真的来自 res.render
:
Yes, there are sync parts of the res.render API (which sucks), but it will be addressed in Express 5.0, as we cannot address it without breaking the view engine compatibility.
Starting your application with NODE_ENV=production or setting the cache to true for rendering will cause file system activities only once per view at startup, which makes this a non-issue while the application is fully running in production, since no sync file systems are called since the views are cached.