Reputation: 2329
I try to make this router response asynchronous:
var express = require('express'),
router = express.Router();
router.get('/', function(req, res, next) {
res.render('contact', {
titleShown: true,
title: 'Contact'
});
});
I tried to implement async
that I read about here, but not working:
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);
});
How can I do that?
Error message that I got when I use the --trace-sync-io
flag:
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)
Upvotes: 0
Views: 3023
Reputation: 2329
No, res.render
is not fully asynchronous (at the moment). So the error is really coming from 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.
Upvotes: 2