Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express - Multiple angular apps

I have express hosting an angular application right now, and it works as it is supposed to. However, I was considering making a new app and hosting it at a different route. I could not get this to work - even on the route to the new app, the old app opens. I'm guessing it has something to do with the static files, but I couldn't solve the issue. Here's how I'm trying to implement it -

// ...
// The usual express code
// ...
app.use('/', express.static(path.join(__dirname, '../client/dist'))); // for the original app
app.use('/admin/', express.static(path.join(__dirname, '../admin/dist'))); // for the new app
app.get('/admin/*', (req, res) => {
  res.sendFile(path.join(__dirname, '../admin/dist/index.html')); // Serve the new app
}
app.get('/*', (req, res) => {
  res.sendFile(path.join(__dirname, '../client/dist/index.htm;')); // Serve the old app
}
// ..
// The remaining code
// ..

The routes seem to be working fine when I use res.send('something');, but it serves the old page whenever I try to use sendFile. Is there any way to have multiple angular apps and serve them simultaneously?
I'm using angular4, and node v6.9.3

like image 527
Zeokav Avatar asked Feb 15 '26 10:02

Zeokav


1 Answers

I figured out how to make this work. In the index.html of the admin app, make the base tag like this

 <base href="/admin/">

My full html file looks is:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Admin</title>
  <base href="/admin/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <apt-root></apt-root>
</body>
</html> 
like image 161
antonioplacerda Avatar answered Feb 17 '26 23:02

antonioplacerda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!