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

228
Vistas
Serving Angular 2/4 code with express

In angular 1 I was able to have a single project for both front end and back end because I could use the express feature to serve up the front end code

app.use('/frontEnd', express.static('frontend'));

This approach has an error because currently with the angular 4 quick start seed the index.html is not in the root front end folder but the src inside the root front end folder. So to account for that I tried:

app.use('/frontEnd', express.static('frontend/src'));

That "works" but when you pull up localhost:8080/frontEnd it breaks.

QUESTION: What is the work around for this?

ALTERNATE QUESTION: If this is no longer available, 1. is there a way to have a single project for both ends or 2. Is the proper procedure to just keep both side separated in their own projects?

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

0

Because of History API, Express need to catch all request made and serve the index.html then let Angular handle routes.

So the snippet you should use in your Express app is :

// Include the path module
include path from 'path'

// Specify your static path
app.use('/assets', express.static(path.join(__dirname, 'frontend/src/assets')));

// Then catch all requests made to http://localhost:8080/frontEnd
app.get('/frontEnd/*', (req, res, next) => {
  res.sendFile(path.join(__dirname, 'frontend/src/index.html'));
});

Now when you gonna load static such images, scripts, etc. the path /assets gonna load static from frontend/src/assets. If you don't specify an asset path, every asset request will return index.html !

Additionally, when you'll go prod, think about server-side rendering. :)

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