A
A
Artem Shchurin2016-08-02 14:06:38
JavaScript
Artem Shchurin, 2016-08-02 14:06:38

What is the correct way to return index.html using fs.readFile()?

Good afternoon
There is an index.html page, it itself pulls 2 scripts, 2 style
files All files are loaded, but the css files are empty, and js contain the text of the page
I suspect that I am not rendering the page correctly, because the page is launched without a server, the scripts are in place and work
On the server side:

var http = require('http');
var url = require('url');
var fs = require('fs');

function onRequest(req, res) {
  res.writeHead(200, {
    "Content-Type": "text/html; charset=utf-8"
  });

  if(req.url = '/'){
    fs.readFile('../clientside/dist/index.html', null, function(error, data) {
      if(error) {
        res.writeHead(404);
        res.write('File not found')
      }else {
        res.write(data);
      }

      res.end();
    });
  }
}
http.createServer(onRequest).listen(1337, '127.0.0.1');

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sasha, 2016-08-02
@madmages

you have only one IF, and only responds to the main page, i.e. "/".
When you try to get another page (for example /some.css), then the request will never be returned, only after the tcp connection timeout, well, that is, we can assume that never. That is, when requesting any page that is NOT / nothing will be returned.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question