node.js - How set start page in nodejs -


i use node js without frameworks etc. , have problem, don't understand how set "start html page" first request server.

i tried this

var server = new http.server(); server.listen(1137, '127.0.0.1');  server.on('request', function(req, res) {     fs.readfile('../public/index.html', function (err, html) {         if (err) {             throw err;         } else {             res.write(html);             res.end();         }      }); }); 

when request 127.0.0.1:1137 - got html in browser, links css/js files isn't correct , how can fix don't know :(

i want html page ../public/index.html in browser when first request server.

my server location project/server/server.js

my html-page location project/public/index.html

your page includes references images , stylesheets, said don't work.

well, responding every single http request contents of specified html page.

when browser parses html, see image , stylesheet links , issue http requests url's. url's don't respond images or stylesheets. respond html.

get /index.html 

yields

<html>   <head>     <link rel="stylesheet" href="styles.css">   </head>    <body>     <img src="someimage.png">   </body> </html> 

the browser requests

get /styles.css 

yields

<html> ... </html> 

the browser requests

get /someimage.png 

yields

<html> ... </html> 

you need make response conditional based on request. prevent disclosure of information, @minitech mentioned, need careful not blindly concatenate paths. have worry mime types.

you're best off using framework express.


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -