Answer the question
In order to leave comments, you need to log in
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);
});
Answer the question
In order to leave comments, you need to log in
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.
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);
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question