Node JS continues to receive Failed to load resource error message

I am currently testing a node.js application and am trying to add:

<link rel="stylesheet" href="assets/css/formalize.css" /> <script src="assets/js/jquery.formalize.js"></script> 

in my code, but saving the error message:

 Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:3000/assets/css/formalize.css Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:3000/assets/js/jquery.formalize.js 

Any idea where I'm wrong? Here is my code so far (in app.js)

 var express = require('express'), app = express(), server = require('http').createServer(app), io = require('socket.io').listen(server); server.listen(3000); app.get('/', function(req, res) { res.sendfile(__dirname + '/index.htm'); }); io.sockets.on('connection', function(socket) { socket.on('send message', function(data) { io.sockets.emit('new message', data); }); }); 
+4
source share
1 answer

You need to add express.static to your app and point it to the directory with static assets.

For example, if the directory structure is configured as follows:

 app.js index.htm static assets js jquery.formalize.js css formalize.css 

You must specify your static resource directory as follows:

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

And link to your files in the html file as follows:

 <link rel="stylesheet" href="/assets/css/formalize.css"/> <script src="/assets/js/jquery.formalize.js"></script> 
+10
source

Source: https://habr.com/ru/post/1492601/


All Articles