Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

229
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!