D
D
Dmitry Rotaty2014-05-10 20:32:11
css
Dmitry Rotaty, 2014-05-10 20:32:11

Node js, How to correctly connect scripts, pictures, etc.?

Hello. Recently I began to study node js, I ran into a problem that I can’t get around for some time. There is a server written in js with stuffing taken from the first available resource:

var express = require("express");
var logfmt = require("logfmt");
var fs = require('fs');
var index = fs.readFileSync('Framework/index.html');
var app = express();
app.use(logfmt.requestLogger());
app.get('/', function(req, res) {
    res.end(index);
});
var port = Number(process.env.PORT || 5000);
app.listen(port, function() {
    console.log("Listening on " + port);
});

at Framework/index.html there is a view that does not display strips and pictures connected to it. With all this, the index.html file opens properly in the browser, the scripts work. But on a local server, no way. More details can be found here , this is, in fact, the project itself.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Eugene Obrezkov, 2014-05-10
@Flamine

How can I serve statics from several directories?
You may typically use any middleware several times within your application. With the following middleware setup and a request to "GET /javascripts/jquery.js" would first check "./public/javascripts/jquery.js", if it does not exist then the subsequent middleware will check "./files/javascripts /jquery.js".
app.use(express.static('public'));
app.use(express.static('files'));
FAQ
You need to register the public folders where the assets to upload are located.

B
Boris Chumichev, 2014-05-12
@BorisChumichev

The problem is that the current implementation of your server does not provide for the return of static resources (images, scripts, etc.). The only request your server can answer is described in this route (lines 7-9):

app.get('/', function(req, res) {
    res.end(index);
});

For any other request, the server will respond with a 404 code error, including a request to get a static resource.
The express module extends the functionality of the connect module, which has a 'static' middleware handler that allows you to set the path where your server will return static resources.
If the resources are located in the "public" folder, then the middleware can be connected with the following line of code:
When using middleware handlers, it should be understood that they are executed in the same order in which they are written in the code. Each middleware can execute one of three scenarios:
1. Break the chain of middleware handlers by responding to a user request.
2. Transform the response and/or request object and continue executing the chain of midleware handlers.
3. Do nothing (just pass control to the next middleware)
Considering that static executes the first or third scenario, it is most likely correct to put the static middleware at the beginning of the chain in order to immediately exclude the execution of subsequent middleware if the client requests static.
Documentation for static middleware:
www.senchalabs.org/connect/static.html

T
Tolik, 2014-06-26
@Diel

If you still need an answer, write in a comment. I make my server on a node and it can send files to the browser (not on Express.js)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question