Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS and angular 2 project

I want to create app with nodeJS and angular2 as client side framework. I have created node project and in public folder create whole angular project with angular-cli. I have routes in nodeJS

// api routes
app.get('/api/getSomeData', function(req, res){
    res.json({'data': 'Dummy Data'});
});

// Redirect all other routes to angular index file
app.get('*', function(req, res){
    res.sendFile(__dirname + '/public/src/index.html');
});

I cant serve angulars index.html page. I dont know if i must build angular and how to do build.

like image 454
Nemanja Avatar asked May 19 '26 17:05

Nemanja


1 Answers

I get it. After ng build command in angular root folder ( node/public ) and next line of code in nodes main file ( my is server.js ) all is good and angular is loaded as is should.

app.use(express.static(__dirname + '/public/dist'));

and node route

app.get('*', function(req, res){
    res.sendFile(__dirname + '/public/dist/index.html');
});
like image 89
Nemanja Avatar answered May 21 '26 07:05

Nemanja



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!