我正在处理我的第一个Angular应用程序并且我遇到了路由问题,因为应用程序响应了"无法获取/(网址)"错误。控制台日志是空的,所以在阅读了许多类似的主题后,我怀疑server.js的问题:
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
var http = require('http');
var app = express();
var api = require('./server/api.js');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname, 'dist')));
app.use('/', api);
app.get('*'), (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'));
}
var port = process.env.PORT || '3000';
app.set('port', port);
var server = http.createServer(app);
server.listen(port, () => console.log("Server is running"));
在我的index.html中,我有:
<base href="/">
所以这不是问题的根源。角度路由代码很好,我已在其他应用程序上测试过它。我很感激你的帮助。
APP-routing.module:
import {NgModule} from "@angular/core";
import {RouterModule, Route} from "@angular/router";
import {Pgee2017StatsComponent} from "./pgee2017/pgee2017-stats/pgee2017-stats.component";
const APP_ROUTES : Route[] = [
{ path: 'pgee17', component: Pgee2017StatsComponent}
];
@NgModule({
imports: [
RouterModule.forRoot(APP_ROUTES)
],
exports: [
RouterModule
]
})
export class AppRoutingModule {}