node.js - Is writeHead a non blocking IO command? -


i writing code load page client. piece of code is:

response.writehead(301, {"location": "/login/index.html"}); response.end(""); 

the question if have slow client called a, code block event loop other clients? or can handle other events while waiting page loaded?

i read in book it's better use pipe method, following code:

var fs = require('fs'); var rs = fs.createreadstream('/login/index.html'); rs.pipe(response); 

which 1 better used?

as @spotirca has pointed out, response.writehead() isn't cached, think it's worth expanding on other aspect of question, use of pipe().

pipe() red herring here, issue whether redirect use existing http response or start new cycle. it's important understand 2 solutions different things.

301 redirect

response.writehead(301, {"location": "/login/index.html"}); response.end(""); 

this responds browser permanent redirect, i.e. tells browser current url should always redirect specified url. if you're redirecting user login page based on session state absolutely wrong, browser justified in caching redirect , causing undesirable behaviour.

streaming

require('fs').createreadstream('/login/index.html').pipe(response); 

this work, mean login page served on whatever current url is. you'd breaking standard http behaviour serving different content on same url, @spotirca points have repercussions around caching.

my suggestion

use 302 (temporary) redirect /login/ serve content there.

response.writehead(302, {"location": "/login/"}); response.end(""); 

i never include file extentions on urls because reduces ability change things later, aspect of approach mean you'd need have request handler listening on url , serving static file. use pipe() , set suitable cache headers, advice use express or similar framework avoid bugs associated custom code.

e.g.

// framework of response.writehead("expires","thu, 01 dec 2994 16:00:00 gmt");  require('fs').createreadstream('/login/index.html').pipe(response); 

Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -