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

100
Vistas
Nodejs public folder always fails silently

I'm trying to include an icon as part of my website, currently my code looks like the following:

app.js

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

const hostname = '127.0.0.1';
const port = 3000;
const html_file = "./index.html";

var app = express()
app.use(express.static(path.join(__dirname, 'public')));
//app.use(express.static('public'));
console.log(path.join(__dirname, 'public'));

fs.readFile(html_file, function (err, html)  {
    if (err) {
        throw err;
    }
    const server = http.createServer((req, res) => {
      res.statusCode = 200;
      res.setHeader('Content-Type', 'text/html');
      res.write(html);
      res.end();
    }).listen(port);
console.log(`Listening on http://${hostname}:${port}`)
});

While my html file looks like this:

<!DOCTYPE html>
<html>
<head>
    <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <body>
<p>test text</p>
    </body>
</html> 

However, the icon is never loaded properly. I can see that the request returns in the network inspection tab, and there are no errors from nodejs in the console, but no matter what it fails.

I've tried switching between adding and not including the /public/ in the link line too, as well as moving the HTML file to the public folder itself.

Any ideas?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You're starting a vanilla HTTP server that only serves your index.html file and nothing else.

I'd suggest moving index.html into the public folder and using app.listen() to start your Express app instead.

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

const app = express();
const port = 3000;

app.use(express.static(path.join(__dirname, "public")));

app.listen(port, () => {
  console.log(`Listening on port ${port}`);
});

I highly recommend working through the Express Getting Started guide.


You should also use an absolute path to your icon in the HTML

<link rel="icon" type="image/x-icon" href="/favicon.ico">
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