DR01D
DR01D

Reputation: 1365

Using node.js express, how to serve files from sibling directory

Background: I've tried what feels like every combination of variables but I can't figure out how to get my server.app file to use a sibling directory to call index.htm and other files. It works fine when it's set up as a subdirectory but I can't figure out how to get this to work as siblings.

Question: What is wrong with my file structure or calls in express?

Folder structure:

A directory named www holds two sub-directories that are siblings.

Directory #1 www/server/server.js

Directory #2 www/html/index.htm

Inside server.js I include these two commands.

app.use(express.static('www'));

app.get('/', (request, response) => {
    response.sendfile('/html/index.htm');
});

When I access the root I get this error in my browser.

Error: ENOENT: no such file or directory, stat '/html/index.htm

Both www/server/server.js

and www/html/index.htm exist.

Thanks so much for any help!

Upvotes: 1

Views: 1250

Answers (1)

Vitaly Migunov
Vitaly Migunov

Reputation: 4457

You need to rename you .htm file to .html.

Even if they are the same express.static wont look for .htm

And then use it like this

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

app.get('/',(req, res) => res.sendFile('index.html'));

Upvotes: 1

Related Questions