Angular - Routing is not working (MEAN)

生来就可爱ヽ(ⅴ<●) 提交于 2021-01-27 17:10:39

问题


I'm working on my first Angular app and I have a problem with routing, because app response with "Cannot GET /(url)" errors. Console logs are empty, so after reading many similar topics I suspect issue with 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"));

In my index.html I have:

<base href="/">

so this is not source of the problem. Angular routing code is good, I've tested it on other app. I would grateful for some help.

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 {}

来源:https://stackoverflow.com/questions/48844931/angular-routing-is-not-working-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!