javascript - How does a Node.js server process requests? -
let's assume have following code. i'm using expressjs, don't think server part different vanilla node.js.
var express=require('express'); var settings=json.parse(fs.readfilesync('settings.json','utf8')); // run once (when server starts)? app.get('*',function(req,res){ res.write(fs.readfilesync('index.html')); // block other requests? settimeout(function(){ someslowsynctask(); // block other requests? },1000); res.end(); });
in example above, first readfilesync
run once when server starts, or run each time server receives request?
for second readfilesync
, prevent node processing other requests? in other words, other requests have wait until readfilesync
finishes before node processes them?
edit: added settimeout
, someslowsynctask
. block other requests?
you should avoid synchronous methods on server. available convenience single user utility scripts.
the first 1 runs once since synchronous method. * route not setup until after returns.
the second run when http request comes server. , yes, block whole server duration of synchronous call (i/o cost open , read contents of file). don't that.
there plenty of articles on internet around understanding node event loop. example, here , here
Comments
Post a Comment