Tuesday, 15 April 2014

node.js - NodeJS url request, absolute or relative? -



node.js - NodeJS url request, absolute or relative? -

a simple question bothers me lot.

what's difference between next 2 cases ?

index.html - script src="script/a.js" - script src="/script/a.js" // starting slash

and why server can serve request (starts slash)

app.get('/script/a.js', function(req, res){ // slash res.sendfile(__dirname + '/realfolder/script/a.js'); });

no matter url src on client side case of 2 cases mentioned ?

on other hand, got 404 error if serve request in next way (starts without slash)

app.get('script/a.js', function(req, res){ // without slash res.sendfile(__dirname + '/realfolder/script/a.js'); });

in opinion, path starts '/' means root folder of application , other means relative path __dirname. , couldn't understand why server can't handle app.get('script/a.js') request without slash in origin ?

anything wrong ?

when path starts slash / means absolute path. when doesn't start slash, relative path.

lets see example. imagine hard disk has next folders:

main subfolder1 subfolder2 lastfolder

now imagine in folder subfolder2 , want load file within lastfolder. can load relative path:

lastfolder/file.txt

but can utilize absolute path:

/main/subfolder2/lastfolder/file.txt

both paths correct, relative 1 can fail if move different folder (for illustration if in subfolder1), while absolute path right (if don't modify folders of course).

node.js

No comments:

Post a Comment