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

232
Vistas
Webpack, Dev-Middleware, and Static Files

I've got a Webpack/React/Redux project served with Express and I'm having some trouble understanding how they fit together. My Express app runs Webpack and serves my root index.html file like so:

const app = express();
const server = require("http").createServer(app);

app.use(bodyParser.json());
app.use("/some/path", express.static(path.join(__dirname, "/public")));

// webpack middleware
const compiler = webpack(webpackConfig);

const webpackDevMid = require("webpack-dev-middleware");
const webpackHotMid = require("webpack-hot-middleware");

app.use(webpackDevMid(compiler, {
    noInfo: true,
    publicPath: webpackConfig.output.publicPath  // '/static/'
}));

app.use(webpackHotMid(compiler));

app.get("/", (req, res) => {
    if (!req.cookies.access_token) return res.redirect("/login");
    return res.sendFile(path.join(__dirname, "index.html"));
});

My root index file then has the "root" tag in the body and the Webpack "/static/bundle.js" in a script tag. The root tag points to my index.js file bundled in bundle.js and everything renders correctly. This all works fine.

My issue is that I'm trying to include my favicon.ico in my root index.html, which is not bundled up with Webpack, so I want to serve it as a static file with Express, which I usually do with the code:

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

Unfortunately, this folder is not getting served to the client, and I cannot access my favicon outside of the Webpack bundle (which my index.html has no discretionary access to). In fact, nothing I try to expose to the client in this way is working. Is there something I'm not understanding about webpack-dev-middleware, or the way an Express server works with Webpack? What gives?

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

In your webpack file make sure you have:

target: 'node',
node: {
    __dirname: false,
},

See:

  • https://github.com/webpack/webpack/issues/1599
  • https://github.com/webpack/docs/wiki/configuration#node

Alternatively, usepath.join( '.' )

about 4 years ago · Santiago Trujillo Denunciar

0

just set the static folder like this

app.use(express.static(__dirname + '/path-to-static-folder'));
about 4 years ago · Santiago Trujillo 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