Ad

Can't Get Local File Test.txt Using Http://localhost:8080/test.txt

- 1 answer

I've got a server running on localhost:8080, and my files are arranged like this:

/test
    server.js
    chatroom.html
    test.txt

server.js:

var express = require("express");
var app = express(); 
app.get("/", function(request, response) {
    response.sendFile(__dirname + "/chatroom.html");
});
app.listen(8080);

So in my browser when I go to http://localhost:8080 I get served chatroom.html just fine.

The problem I'm having:

But when I go to http://localhost:8080/chatroom.html it shows me "Cannot GET /chatroom.html"

And, likewise, if I go to http://localhost:8080/test.txt it shows me "Cannot GET /chatroom.html"

I don't understand what's going wrong here, any help would be appreciated!

Ad

Answer

You've only defined one path for Express, the root path "/". If you want other files available you need to call app.get() for them as well.

Or, you can put those static assets in a directory and use express.static to serve them.

Ad
source: stackoverflow.com
Ad