How does routing in conjunction angular2 + express?

Study routing in angular2, had previously worked with Express. Learned how to set the path, how to set 404 error if there's no way. In General, all the same I did before but only to Express. Interested in how they will do in terms of routing to communicate with each other, if I have a MEAN stack for example. Is it enough to set only in angular or something to Express will have to do then?
July 2nd 19 at 17:34
2 answers
July 2nd 19 at 17:36
And how do you plan to use express ? MEAN stack implies that your backend is only the REST API, if I am not mistaken. Therefore, you have all the requests, for example, go to express and there he resolves ranting, and all other requests go to the client application and there resolves angular.
July 2nd 19 at 17:38
suppose the application consists of back and front dads who Campiglio in /dist/*

let root = process.cwd();
 let nodeModules = '/node_modules/';
 let clientFiles = '/dist/front';

 application.use(express.static(path.join(_root , nodeModules)));
 // custom express, indicating the link to the folder with the build of the frontend so
 application.use(express.static(path.join(_root , clientFiles)));
 // now the Express will know where to look for static, i.e., our front + node_modules folder

 // konfigurim API router 
 router = express.Router();

 // hide all the calls to the api in a separate branch
 application.use('/api', router);
 // API error will process your JSON response
 application.use('/api', function (error: any, req: express.Request, res: express.Response, next: express.NextFunction){
Res. status(statusCode).json({error});

 application.all('*', (req: any, res: any) => { // everything else is sent in dist/front/index.html/angularRoute
 Res. status(200).sendFile(path.join(root, clientFiles, '/index.html'));

The trick is that your api does not know your front-ovyh of ranting.
So when a express falls route: '/users/1' and it doesn't find one, it sends it to index.html => index.html/users/1 , now angular looks whether it be Routh, and if not, then draws the page with the error.
However, the api is purely back-andovska ranting, respectively, for this thread, we errore handler was hanged.

Find more questions by tags Express.jsAngularNode.js