Answer the question
In order to leave comments, you need to log in
How to correctly send html page to client in NodeJs Express?
I want to send an html file directly to the client, without templates like ejs. To do this, I provide static files, and when requesting a site, I send an html page using the sendFile () method.
app.use(express.static(__dirname + '/public'));
app.get("/", function(req, res) {
console.log("Something was catched!" + req.method);
res.sendFile('public/index.html', {root: __dirname });
});
Answer the question
In order to leave comments, you need to log in
The public also contains the css, js, jpg files that the page uses, and unfortunately the client can access them if I provide this directory.
I don't understand what's wrong with that? If the client does not have access to css, js and other resources, they will stop working, pictures will not be displayed, fonts will be displayed as standard.
Don't store any private data in a folder like /public. The name of the folder speaks for itself - public information.
res.end() does not close the connection, but ends the response to the request. In this case, the connection can remain open if it is keep-alive.
No, wrong. Connect logger like morgan or any other:and read the logs if you're interested.app.use(morgan(':id :method :url :response-time'));
And if the index.html file does not change, it is not necessary to describe it as a special case. express.static() does a lot more than just sendFile(). It tells search engines, for example, the date the file was last modified, in order not to re-index the file, the file must be available not only for a GET request, but also for a HEAD request.
Ajax and connections are completely different levels of abstraction. Connections, if considered at the tcp level, are opened and closed automatically. They are not regulated by the http or https modules. If you need to work with connections, you need the net module. Ajax will work anyway, even if the connection is closed, it will open a new one, again automatically, the browser does not provide an api to the client javascript to manage connections to the server other than WebSocket.
I don’t know exactly the same, but here’s a similar one + apply noise (yourself)
background: -moz-linear-gradient(left, #770c19 0%, #710c1e 12%, #670c29 26%, #510c3f 45%, #260d69 78%, #1b0d73 89%, #150d79 100%); /* FF3.6-15 */
background: -webkit-linear-gradient(left, #770c19 0%,#710c1e 12%,#670c29 26%,#510c3f 45%,#260d69 78%,#1b0d73 89%,#150d79 100%); /* Chrome10-25,Safari5.1-6 */
background: linear-gradient(to right, #770c19 0%,#710c1e 12%,#670c29 26%,#510c3f 45%,#260d69 78%,#1b0d73 89%,#150d79 100%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
Red background, blue elliptical gradient, green linear gradient?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question