Node.js does not load js nor css scripts

2

I am making a chat app and I can get the page I want but this page does not load the corresponding css nor the js scripts, does anyone have a solution for this please? So far I have this:

Code:

server.js:

var socket  = require( './node_modules/socket.io' );
var express = require('./node_modules/express');
var app     = express();
var server  = require('http').createServer(app);
var io      = socket.listen( server );
var port    = process.env.PORT || 3000;
var users = [];

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

io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    io.emit('chat message', 'isto veio do server: ' +msg);
  });
});

server.listen(port, function () {
  console.log('Server listening on port %d', port);
});

index.html:

...
<link rel="stylesheet" type="text/css" href="/styles/estilos.css"> <!-- este não carrega -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.6/socket.io.js"></script><scriptsrc="scripts/funcs.js"></script> <!-- este não carrega -->
...
    
asked by anonymous 03.06.2016 / 17:40

1 answer

2

You have to set up a base directory in your%% of your directory so that html is able to use resources. Put this next to server.js and see if it works:

app.use(express.static(__dirname + '/'));
    
03.06.2016 / 17:42