Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda