D
D
Dead Sea2016-12-31 01:32:41
Node.js
Dead Sea, 2016-12-31 01:32:41

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 });
});

There are a lot of questions, I read a lot, but I still don’t understand the mechanics very well.
Is this approach correct? 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.
What would be the right thing to do when providing a page?
At what point should the connection be closed with the end() method?
If an ajax request arrives from the client from the page, then I separately process it by sending data, and close this connection, but the main connection through which I provided the page should always be open? Please help me understand the logic of this application.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
ckr, 2016-12-31
@Dead_Sea

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:
app.use(morgan(':id :method :url :response-time'));
and read the logs if you're interested.
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.

F
Flur, 2017-06-07
@drtvader

I don’t know exactly the same, but here’s a similar one + apply noise (yourself)
7c89b600009d4c2d8db4cde6b27f64ae.png

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+ */

S
Sergey Sokolov, 2017-06-07
@sergiks

Red background, blue elliptical gradient, green linear gradient?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question