我已经构建了一个客户端服务器应用程序,并且使用MongoDB作为我的数据库。
我开始之后
npm运行构建
和
npm run start:prod
我能够加载正常运行的网站,但是刷新页面后,我会得到
无法获取/开始
我的应用程序结构如下
routing.ts
const routes: Routes = [
{ path: '', redirectTo: 'start', pathMatch: 'full' },
{ path: 'start', component: StartComponent },
{ path: 'ai-option', component: OptionComponent },
{ path: 'deepdata', component: DeepdataComponent },
{ path: '**', component: StartComponent }
];
export const routing = RouterModule.forRoot(routes);
服务器
import * as express from 'express';
import * as bodyParser from 'body-parser';
import * as path from 'path';
import * as cors from 'cors';
import { Routes } from './routes/twitter';
class App {
public app: express.Application;
public route: Routes = new Routes();
constructor() {
this.app = express();
this.config();
this.route.routes(this.app);
}
private config(): void {
this.app.use(cors());
this.app.use(bodyParser.json());
this.app.use(bodyParser.urlencoded({ extended: false }));
if (this.app.get('env') === 'production') {
// loading the angular client folder or application
this.app.use(express.static(path.join(__dirname, '/../client')));
}
}
}
export default new App().app;
package.json脚本
"scripts": {
"ng": "ng",
"start": "concurrently --kill-others \"npm run server:run\" \"ng serve --proxy-config proxy.conf.json\"",
"server:run": "tsc -p ./server && concurrently \"tsc -w -p ./server\" \"nodemon dist/server/server.js\" ",
"build": "ng build --prod --output-path=dist/client && npm run server:build",
"server:build": "tsc -p ./server",
"copyfiles": "cp -R ai dist/server/routes",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"start:prod": "NODE_ENV=production node dist/server/server.js"
},
该应用程序在localhost:8000下运行
我的代码怎么了?
答案 0 :(得分:0)
您需要将所有请求重定向到angular到文件index.html。像这样:
router.use('*', (req, res) => {
res.redirect('angular-dir/index.html');
})
此代码需要在文件末尾添加路由器文件。