I am building a basic web application with Express 4 and Angular 2. There is nothing special about Angular 2, except that I use its HTML 5 router.
Here is an example of application routing:
There are two main server-side routing configurations. Both look something like this:
router.get('/', authenticationHelpers.isAuth, function(req, res, next) {
res.render('index');
});
router.get('/login', authenticationHelpers.isNotAuth, function(req, res, next) {
res.render('login');
});
They explicitly control two cases in which the user goes to /and /login.
However, if the user is logged in and can visit /to visualize the express express view, there are HTML5 routes that the user can use. These include the following URLs:
localhost:5000/user
localhost:5000/profile
localhost:5000/profile/settings
Problem
It’s clear that there isn’t router.get('/user'), and this shouldn’t be, as this is all the interface work performed by the Angular 2 router. Nevertheless, to include a link that allows the user to simply type localhost:5000/profile/settingsand redirect the site’s route to the index file (provided that you logged in) and THEN route you (using Angular 2 HTML 5 routing) to your own /profile/settingsI had to put this piece of code in mine app.js:
app.all("/*", function(req, res, next) {
res.render('index');
});
However, this is a big problem. If you are not logged in and provided with a link localhost:5000/profile/settings, it will display the index view, since it only runs the function authenticationHelpers.isAuthin the routing code router.get('/')above. I would also like you to have 404 errors on routes that do not exist in the expression, or angular., , express HTML 5, -. , , HTML 5 Angular, ( , ). , , . - , ! Angular - - , , :
router.get(['/', '/user/', '/profile/*'], authenticationHelpers.isAuth, function(req, res, next) {
res.render('index');
});