Angular 4
I'm getting the Uncaught SyntaxError: Unexpected token <
error from my bundles, but only in a specific situation:
- on live server only (nginx forwarding all traffic to express server, config below)
- only when refreshing routes that end on a route param, e.g.
chart/123
, where123
is a chart id and the path ischart/:id
in the router. Something like/profile
works fine as this isn't a param in a route. - edit - I also get this error for routes such as
users/add
andorganisations/add
, so it seems to error for any route with more than one 'part'...
The result is that all the *.bundle.js
requests are returning the index.html
file instead of their preferred *.js
files.
Any ideas? Let me know if more context is needed.
Thanks in advance for any help.
Here is my server.js:
const express = require('express');
const compression = require('compression');
const morgan = require('morgan');
const path = require('path');
const rfs = require('rotating-file-stream');
const fs = require('fs');
const app = express();
/**
* Logging
*/
/**
* Create a logs directory (in project root)
*/
const logDirectory = path.join(__dirname, './logs');
if (!fs.existsSync(logDirectory)) {
fs.mkdirSync(logDirectory);
}
// Set up rotating file stream, rotate every day
const accessLogStream = rfs('access.log', {
interval: '1d',
path: logDirectory,
});
// winston.info(logDirectory);
app.use(morgan('short', { stream: accessLogStream }));
/**
* /Logging
*/
app.use(compression());
const port = process.env.PORT || 8082;
app.disable('x-powered-by');
app.use(express.static(__dirname + '/dist'));
app.get('*', (req,res) => {
try {
res.sendFile(`${__dirname}/dist/index.html`);
} catch (e) {
res.status(404).send('index not found');
}
});
app.listen(port,'127.0.0.1');
console.log(`listening on 127.0.0.1:${port}`);
Here is my angular-cli.json (pretty much boilerplate):
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"project": {
"name": "new-client"
},
"apps": [
{
"root": "src",
"outDir": "dist",
"assets": [
"assets",
"favicon.ico"
],
"index": "index.html",
"main": "main.ts",
"polyfills": "polyfills.ts",
"test": "test.ts",
"tsconfig": "tsconfig.app.json",
"testTsconfig": "tsconfig.spec.json",
"prefix": "app",
"styles": [
"assets/scss/main.scss"
],
"scripts": [
"../node_modules/chart.js/dist/Chart.min.js",
"../node_modules/moment/min/moment.min.js"
],
"environmentSource": "environments/environment.ts",
"environments": {
"dev": "environments/environment.ts",
"livedata": "environments/environment.livedata.ts",
"production": "environments/environment.prod.ts"
}
}
],
"e2e": {
"protractor": {
"config": "./protractor.conf.js"
}
},
"lint": [
{
"project": "src/tsconfig.app.json"
},
{
"project": "src/tsconfig.spec.json"
},
{
"project": "e2e/tsconfig.e2e.json"
}
],
"test": {
"karma": {
"config": "./karma.conf.js"
}
},
"defaults": {
"styleExt": "scss",
"component": {
}
}
}