Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

175
Visualizações
How do I configure my Express server to render all of my content, not just my HTML file?

I'm attempting to build a server for my current project but I'm new to Express. My file directory looks like this:

root-directory
├── public
│   ├── css
│   │   └── styles.css
│   ├── images
│   │   └── img1.png
|   |   └── img2.png
|   |   └── img3.png
│   └── index.html
|   └── main.js
└── server
    └── app.js

This is the code I am using in my server:

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

const app = express();
app.use('/static', express.static('public'));
app.use(express.json());

app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, '../public/index.html'))
});

const port = 5000;
app.listen(port, () => console.log(`Server is listening on port ${port}.`))

Currently, the server is only displaying my static HTML file, and none of the JavaScript or CSS that is linked to it. I have attempted altering my sendFile function to look like this: res.sendFile(path.join(__dirname, '../public')); but that results in an Error: Cannot GET /.

How can I reconfigure my server to display my HTML, CSS, and JavaScript all at once?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

  • Firstly, you can remove this block:
app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, '../public/index.html'))
});

You don't need to respond to all GET requests (because of the use of '*') with the index.html file. Moreover, it conflict with the line app.use('/static', express.static('public'));.

  • Secondly, you need to replace /static in this line with /
app.use('/static', express.static('public'));

Then your app will find index.html in the public folder to respond to the GET '/' request.

  • Thirdly, you need to check your index.html file if the link to CSS/JS/image files is valid. For example, the link to style.css file should be: <link rel="stylesheet" href="/css/style.css">.
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda