Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node JS - HTML Paths

I recently have the problem that I dont get how the paths for html in node js work. I link my index.html's scripts as normal - relative to the index.html's file (node.js file and index.html are in the same directory "res.sendFile(__dirname + '/index.html');"). But if I open it up in the Browser executed with node js it just stats "cant GET blabla" for the scripts. Instead opening it up by just clicking index.html without node js those paths work! How do I have to write html paths for node js?

var express = require('express'),
    app = express(),
    server = require('http').createServer(app),
    io = require('socket.io').listen(server),
    port = Number(process.env.PORT || 3000),

server.listen(port);

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

Thanks for your time! :)

like image 482
dunnohowishouldnamemyself Avatar asked Mar 22 '16 12:03

dunnohowishouldnamemyself


1 Answers

Look at this:

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

You have told Node "When the browser asks for / give it index.html".

What happens when the browser asks for someScript.js?

You haven't told Node what to do then.

(You'll probably want to find a library for serving up static files rather than explicitly handling each one individually).

like image 186
Quentin Avatar answered Oct 23 '22 14:10

Quentin