Hello,

I have such a problem, I can't upload style files and scripts on the server.

file tree:

--bootstrap --css --js ---libs 

in the console gives an error

 localhost/:8 GET http://localhost:3000/bootstrap/css/bootstrap.min.css localhost/:9 GET http://localhost:3000/css/template.css localhost/:11 GET http://localhost:3000/js/libs/jquery%201.12.5-pre.js localhost/:12 GET http://localhost:3000/bootstrap/js/bootstrap.min.js localhost/:14 GET http://localhost:3000/js/libs/angular.min.js localhost/:15 GET http://localhost:3000/js/libs/angular-route.min.js localhost/:16 GET http://localhost:3000/js/libs/angular-resource.min.js localhost/:17 GET http://localhost:3000/js/libs/angular-cookies.min.js localhost/:19 GET http://localhost:3000/js/app.js localhost/:20 GET http://localhost:3000/js/controllers.js localhost/:21 GET http://localhost:3000/js/directives.js 404 (Not Found) 

server.js code

 var express = require("express"); var app = express(); app.use(express.static('TestApp')); app.use(express.static(__dirname + '/TestApp')); app.get('/', function(req,res) { res.sendFile(__dirname + '/index.html'); }); var server = app.listen(3000, function() { console.log('working on', server.address().port); }); 

I tried to prescribe it, but then another problem arises, and it also does not load anything

 app.get('*', function(req,res) { res.sendFile(__dirname + '/index.html'); }); 

    1 answer 1

    You incorrectly use express.static .

    Calling:

     app.use(express.static('TestApp')); 

    you tell Express.js that all static files should be searched in the TestApp directory, but judging by your directory tree, this is not the case.

    I would advise you to move all of your static files (given to clients) to one top-level directory, like this:

     node_modules/ static/ bootstrap/ css/ js/ libs/ index.html server.js 

    Then the server.js code can be:

     var express = require("express"); var app = express(); app.use(express.static('static')); var server = app.listen(3000, function() { console.log('working on', server.address().port); }); 
    • thanks, it worked, I just study it myself, not everything is clear, can you advise what you can read? - daydreams
    • In my opinion, the official documentation for express is quite enough. - Dmitriy Simushev