I
I
Ingerniated2018-03-22 18:53:51
Node.js
Ingerniated, 2018-03-22 18:53:51

Why does fs.open() not send html?

Good evening, please advise.
1) Why, when opening an html file, css is not opened along with it?
2) Why doesn't he open the page through fs.open?

var file = fs.open("index.html", "r");
res.writeHead(200,{"Content-type":"text/html"});
res.write(file);
res.end();

3) How to connect the style in this situation?
Does this require a separate function immediately after the first response with our html file fires?
And how can I track this request in the console, why css did not open and see an error?
var http = require('http');
var fs=require('fs');
var httpServer = http.createServer(function (req, res) {
var data = fs.readFile("index.html", function(err, file) {
    res.writeHead(200,{"Content-type":"text/html"});
    res.write(file);
    res.end();
});
}).listen(8000);

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="main">,/div>
</body>
</html>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2018-03-22
@Ingernirated

1) Why, when opening an html file, css is not opened along with it?

The browser requests them, but your "server" only returns the index.html file
You opened a file for reading, received a file pointer to the file variable, and then you try to send this pointer over the network
Use human tools, node-static for example.
How to serve static server to node.js ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question