1

On Replit, rendering the html file using res.sendFile inside a app.get works perfectly fine, AND I am able to add logos, styles, and js logic file by passing in express.static middleware. BUT when I try to also include the html as a static file passed to express.static middleware, the page does not render.

Here's the replit: https://replit.com/@yanichik/NodeJSandExpressFullCourseFCC#02-express-tutorial/app.js

Renders as expected when html passed in with res.sendFile:

const express = require('express'); const path = require('path');

const app = express();

// setup static & middleware // static -> file that server does NOT have to change app.use(express.static(path.resolve(__dirname, './public')))

app.get('/', (req, res) => { res.sendFile(path.resolve(__dirname, './navbar-app/index.html')) })

app.all('*', (req, res) => { res.status(404).send('Resource not found.') })

app.listen(5000, () => { console.log('Server listening on port 5000...') })

module.exports = app;

enter image description here

Now, DOES NOT render as expected when html passed in with express.static middleware:

const express = require('express'); const path = require('path');

const app = express();

// setup static & middleware // static -> file that server does NOT have to change app.use(express.static(path.resolve(__dirname, './public')))

// app.get('/', (req, res) => { //
res.sendFile(path.resolve(__dirname, './navbar-app/index.html')) // })

app.all('*', (req, res) => { res.status(404).send('Resource not found.') })

app.listen(5000, () => { console.log('Server listening on port 5000...') })

module.exports = app;

enter image description here

1 Answer 1

0

You have to specifically request for the statically exposed files like so:

https://baseURL.com/navbar-app/index.html

When you comment out get routes.

If you have your get route uncomented route then

https://baseurl.com

Will return the html file

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

1 Comment

Sanket, that's not really working. Still getting the same thing. Exposing the public folder to capture static files does work for me for capturing the non-html files (css, js, and svg) and you can see it works, but it doesn't work for the html file. And it should work the same way. I am following along this course (youtube.com/watch?v=Oe421EPjeBE at time 5:15) and really doing the same thing but it doesn't seem to work in Replit, so not sure if there is something different that needs to be done in Replit.

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.