0

I'm very new to NodeJS, and I'm currently playing around with it (and websockets), so this question might be a bit dumb. Anyway, I'm following a tutorial which has given me a simple app.js containing the following:

var fs = require('fs')
, http = require('http')
, socketio = require('socket.io');

var server = http.createServer(function(req, res) {
    res.writeHead(200, { 'Content-type': 'text/html'});
    res.end(fs.readFileSync(__dirname + '/index.html'));
}).listen(8080, function() {
    console.log('Listening at: http://localhost:8080');
});

socketio.listen(server).on('connection', function (socket) {
    socket.on('message', function (msg) {
        console.log('Message Received: ', msg);
        socket.broadcast.emit('message', msg);
    });
});

In my index.html I'm trying to load some js and css files, but I can't seem to load them. The files are inside a js folder which is in the same directory as my app.js and index.html, and I'm trying to load them like so:

<script src="/js/script.js"></script>

If I look at the response from the request in my browser, it's returning the content of index.html.

Again, sorry if this question is silly, but I'm stuck and have no clue where to look.

Thanks!

3 Answers 3

7

A web server in node.js does not serve ANY files by default (unlike some other web servers). So, if you want js files to be served, you have to define a web server route that will serve them. The code you show returns index.html for all incoming requests coming into that http server so, it should be no surpise that when a request comes in for /js/script.js, your web server sends out index.html.

A typical framework to use with node.js for web serving is Express and it has express.static() that can be used to define a route that will cover all your static files or all files in a particular directory. You could, of course, code your own static file handling or find some other module to do that also. The point is that you have to write or configure some code to serve your static resource files. That is not done for you automatically by the node.js http server.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that's very helpful, I'll have a look at Express!
1

you can specify to the server in which folder to look for what

for static files such as css, images you can use public directory, you can provide your custom directory, but it's better to use public ,same goes for views

always require

const PATH = require('path')

app.use(express.static(PATH.join(__dirname, 'public')));

for template files such as .ejs, .html, .jade use

app.set('views', PATH.join(__dirname, 'views'));

2 Comments

app.use('/main/static', express.static(path.join(__dirname, 'public'))); "main" is the main uri defined in node js selector. This solved my issue.
This will error because app, express, and PATH are all undefined.
-1

I used this.

app.use('/main/static', express.static(path.join(__dirname, 'public')));

"main" is the default uri defined in node js selector. This solved my issue

3 Comments

This will error because app, express, and path are all undefined.
This is how i solved my issue. I created nodde js app in cpanel and i am able to serve static files like this
Your issue is not the same as the one being asked about then.

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.