0

I Understand many people have asked the same question but i just can't get this to work for me at all.

I am trying to make a socket.IO application and are following the tutorials but just can't get CSS and JS to be loaded to the page. The application just always sends the html page.

My folder structure.

./server.js
./public
    /css
        /styles.css
    /js
        /client.js
    /index.html

My Server.js file contains:

var express = require('express');
const socketIO = require('socket.io');
const path = require('path');

const PORT = process.env.PORT || 3000;
const INDEX = path.join(__dirname, '/public/index.html');

var express = require('express');
var app = express();
app.use(express.static(__dirname + '/public'));

const server = express()
    .use((req, res) => res.sendFile(INDEX) )
    .listen(PORT, () => console.log(`Listening on ${ PORT }`));

const io = socketIO(server);

And in my index.html file I call the css and js like this:

<link rel="stylesheet" type="text/css" href="/css/styles.css" />
<script src="/js/client.js"></script>
3
  • Go in network tab and look at request status Commented Feb 19, 2017 at 12:25
  • how about looking up for the network at chrome devtools? what's the request url and what's the error Commented Feb 19, 2017 at 12:25
  • What errors you get? Are you sure server root is properly set? have you tried relative path includes? <link rel="stylesheet" type="text/css" href="css/styles.css" /> <script src="js/client.js"></script> Commented Feb 19, 2017 at 12:25

2 Answers 2

1

Why use two express variables app and server

Try the following

const socketIO = require('socket.io');
const path = require('path');
const INDEX = path.join(__dirname, '/public/index.html');

const PORT = process.env.PORT || 3000;

var express = require('express');

const app = express()
    .get('/', function (req, res) {res.sendFile(INDEX)})
    .use(express.static(__dirname + '/public'))
    .listen(PORT, () => console.log(`Listening on ${ PORT }`));

const io = socketIO(app);
Sign up to request clarification or add additional context in comments.

3 Comments

How could this work? This installs a middleware that will call res.sendFile(INDEX) for every single route and does not ever call next() so the express.static() middleware will never get called.
@jfriend00 made the correction. Thanks for pointing that out.
Perfect, that works correctly. thanks for your help.
0

This will work:

const express = require('express');
const app = express();
const http = require('http').Server(app);
const socketIO = require('socket.io')(http);
const PORT = process.env.PORT || 3000;

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

http.listen(PORT, () => console.log(`Listening on ${ PORT }`));

Your code doesn't work because .use((req, res) => res.sendFile(INDEX)) always sends index.html, to any request from the client.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.