Не удается получить / начать после обновления страницы - PullRequest
0 голосов
/ 09 апреля 2019

Я создал клиент-серверное приложение и использую MongoDB в качестве базы данных.

После того, как я начну

npm run build

и

npm run start: prod

Я могу загрузить сайт, он работает нормально, но после обновления страницы я получаю

Не удается получить / запустить

Моя структура приложения выглядит следующим образом

  • расстояние
    • клиент
    • Сервер
  • package.json

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

Что не так с моим кодом?

1 Ответ

0 голосов
/ 09 апреля 2019

Вам нужно перенаправить весь запрос в угловой файл file.html. Некоторым нравится это:

router.use('*', (req, res) => { 
  res.redirect('angular-dir/index.html'); 
})

Этот код нужно добавить в файл роутера в конец файла.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...