How to serve folder in 'public' with express
How to serve folder in 'public' with express
I am trying to serve the login page in my public folder in express, so far i have been unable to do so. I tried to work the path but I don't know why it does not work. What I want to have is that I can have more code in the callback but because of the way it is now I can do that, when I try to make the callback a full function it won't work either.
My folder structure
- root
- server
- server.js
- public
- login
- index.html
const express = require('express');
const app = express();
const path = require('path');
app.listen(4000);
console.log('server startedn');
app.get('/', () => app.use(express.static(__dirname + '/public/login')));
// what i want to have
app.get('/', () =>
// serve file
);
that sadly doesn't work, and without __dirname it doesn't either
– R. Nijhuis
Sep 1 at 10:22
Possible duplicate of How to render a HTML page form NodeJs API?
– NAVIN
Sep 1 at 15:07
Request you to search little more before you ask question. See all these post are similar to yours and they answer your question very well. Post 1: stackoverflow.com/questions/52122677, Post 2: stackoverflow.com/questions/51967362, Post 3: stackoverflow.com/questions/5208321, Post 4: stackoverflow.com/questions/52014410, Post 5: stackoverflow.com/questions/51967761
– NAVIN
Sep 1 at 15:13
@NAVIN none of the posts is related to this question ?! Also the duplicate you flagged has nothing todo with this. Please read questions more carefully before wasting everyones time
– Jonas Wilms
Sep 1 at 19:40
1 Answer
1
You should mount the static file serving directly when the server starts, not after the first request arrives so this line:
app.get('/', () => app.use(express.static(__dirname + '/public/login')));
has to be:
app.use(express.static(__dirname + '/../public/login'));
You also have to go up one folder as the code is started in the /server/
directory, which can be done with /../
.
/server/
/../
If you only want to server the index.html from public folder instead of all files in there, what you can do is
app.get('/', (req, res) => res.sendFile('index.html', { root: __dirname + '/../public/login'));
it doesn't work, i would like to have it as a callback function as well <!-- language: lang-js --> app.get('/', () => app.use(express.static(__dirname + '../public')); ) <!-- end snippet -->
– R. Nijhuis
Sep 1 at 10:27
See... static routes are to serve static files not to listen to an endpoint request(which is not a resource). So if you want to liste to request to home path, what you should actually do is use res.sendFile or res.send with some data
– binariedMe
Sep 1 at 10:37
static files request ends with a file name and extensions
– binariedMe
Sep 1 at 10:39
Can you try the edit part? Remember you should have the correct __dirname path i.e. you should be running server.js file form server/ directory on command line
– binariedMe
Sep 1 at 10:42
i will try it later, busy with other project
– R. Nijhuis
Sep 1 at 10:57
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
change the path to __dirname + '../public'
– binariedMe
Sep 1 at 10:20