I have a "simple" Angular2 app that I want to serve using express. I've set up a very basic implementation, but now I can't get routing to work properly. What happens is that when I navigate to the default route - localhost:8081, the app will load, and all the routing components work.
Then when I go to an angular defined route and refresh the page, I just get the following error: Cannot GET /landing
.
I've added the <base href="/" >
tag onto my index.html file, and I've been able to serve the app properly using npm-lite.
I have setup a github repo in case anyone would like to look at the file structure.
my server.js:
var express = require('express');
var app = express();
app.use(express.static(__dirname));
app.get('/', function (req, res) {
res.render('index.html');
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})
index.html:
<html>
<head>
<!-- angular2 router -->
<base href="/" >
<meta charset="utf-8">
<title>Miha Šušteršič - Portfolio</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- fonts -->
<link href='https://fonts.googleapis.com/css?family=Bree+Serif|Open+Sans' rel='stylesheet' type='text/css'>
<!-- 1. Load libraries -->
<!-- IE required polyfills, in this exact order -->
<script src="node_modules/es6-shim/es6-shim.min.js"></script>
<script src="node_modules/systemjs/dist/system-polyfills.js"></script>
<script src="node_modules/angular2/es6/dev/src/testing/shims_for_IE.js"></script>
<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/rxjs/bundles/Rx.js"></script>
<script src="node_modules/angular2/bundles/angular2.dev.js"></script>
<script src="node_modules/angular2/bundles/router.dev.js"></script>
<!-- 2. Configure SystemJS -->
<script>
System.config({
packages: {
app: {
format: 'register',
defaultExtension: 'js'
}
}
});
System.import('app/main')
.then(null, console.error.bind(console));
</script>
</head>
<!-- 3. Display the application -->
<body>
<portfolio-app>Loading...</portfolio-app>
</body>
</html>
Looking at this question I modified my main.ts app file a bit to look like this, but the problem still persists:
import { bootstrap } from 'angular2/platform/browser';
import { AppComponent } from './app.component';
import { provide } from 'angular2/core';
import {
HashLocationStrategy,
LocationStrategy,
ROUTER_PROVIDERS
} from 'angular2/router';
bootstrap(AppComponent, [
ROUTER_PROVIDERS,
provide(LocationStrategy, {useClass: HashLocationStrategy})
]);
If you need additional info please let me know.