Nodejs Index.html Loads, but 404 links and scripts

I have a clientj nodejs server encapsulated in a client folder to make it easier to manage the folder structure for index.html. Then links and scripts should not load without problems.

client folder /index.html /style.css /script.js closer folder /closure /etc. app.js package.json utility.js 

In my app.js file I have normal

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

and when I start and go to the local host port, the file loads, but when I open the chrome console, I see that the index.html file could not load any of the scripts with 404 error that was not found. I notice that many express apps seem to have a general outline of something like strings

 this app.set('views', __dirname + '/client'); this app.use(express.static(__dirname + "./client")); this app.use('client', express.directory('client')); 

but I see no explanation for the differences between app.use and app.set, nor a good explanation, the best I could find was

app.set ('views', __dirname + '/ views'): use. / views as the default path for client templates

from Alex Young is an article , but even that was a bit lean and dry for me, I hope to explain a little deeper why the index file cannot load the link at the same directory level as it is.

 <link rel="stylesheet" type="text/css" href="style.css" /> 

I look at this and I cannot find the problem.

+6
source share
2 answers

From the answer, Express-js cannot install my static files, why? I would suggest:

 app.use("/client", express.static(__dirname + '/client')); 

This is necessary because your:

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

only refers to what happens when someone goes to / and says that index.html should be served. This alone does not help.

+5
source

just start your server from the root directory of your project. which will fix the problem because you used relative addressing.

-1
source

All Articles