I'm building my first MEAN app, Angular 6. 0.2, Express 4.16.0
Files have been generated thanks to Angular CLI and Express generator
Connection to MongoDB is perfectly running, actually, everything is working BUT the manual refresh...
According to the posts I've already seen, the problem should come from the routers (keeping in mind that I want to use the Angular router).
So far my app.js file has 2 routes:
var indexRouter = require('./routes/index'); // Express generated
var apiRouter = require('./routes/book');
Followed by...
app.use(express.static(path.join(__dirname, 'public'))); // Express generated
app.use(express.static(path.join(__dirname, 'dist/myProjectFolder')));
app.use(express.static(path.join(__dirname, 'dist/myProjectFolder')));
And...
app.use('/', indexRouter); // Express generated
app.use('/api, apiRouter);
After that I have my default error handlers (Express generated).
Index route:
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
module.exports = router;
EDIT 01 — Now have a "Path is not defined" error after applying the first suggested solution.
EDIT 02 — Problem solved thanks to the suggested solution (see comments as well). However router.get('/', function... breaks the access to the data (error 200). If you have the same issue only use specific ones such as router.get('/poney', function... Not / (unless someone knows what to do to fix it).
EDIT 3 — After further investigation, you can have a "magical" router by using
router.get(/^\/(?!api).*/, function(req, res) {
req.sendFile(path.join(__dirname, '.. /path/to/your/index.html'));
});