Angular SSR Heroku部署错误,如何修复

时间:2020-03-16 08:01:27

标签: node.js angular typescript express heroku

我正在将我的角度应用程序(ssr-通用)部署到heroku nodejs服务器,并且出现此错误:

错误:

(函数(导出,需求,模块,__ filename,__ dirname){ 导入'zone.js / dist / zone-node'; 2020-03-15T04:58:31.399518 + 00:00 app [web.1]:^^^^^^^^^^^^^^^^^^^^^^^^^^ 2020-03-15T04:58:31.399519 + 00:00 app [web.1]:2020-03-15T04:58:31.399519 + 00:00 app [web.1]:语法错误: 意外的字符串

当我运行时,一切都在我的计算机上运行:

npm run build:ssr
npm run serve:ssr

这是什么问题?如何解决?

package json:

  "scripts": {
    "ng": "ng",
    "start": "ng serve --port 4200",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "dev:ssr": "ng run devfox:serve-ssr",
    "serve:ssr": "node dist/devfox/server/main.js",
    "build:ssr": "ng build --prod && ng run devfox:server:production",
    "build:stats": "ng build --stats-json",
    "analyze": "webpack-bundle-analyzer dist/devfox/browser/stats-es2015.json",
    "prerender": "ng run devfox:prerender",
    "postinstall": "npm run build:ssr",
    "preinstall": "npm install -g @angular/cli @angular/compiler-cli typescript"
  },
  "engines": {
    "node": "10.15.1",
    "npm": "6.4.1"
  },

server.ts:

import 'zone.js/dist/zone-node';

import { ngExpressEngine } from '@nguniversal/express-engine';
import * as express from 'express';
import { join } from 'path';
import { AppServerModule } from './src/main.server';
import { APP_BASE_HREF } from '@angular/common';
import { existsSync } from 'fs';
import 'localstorage-polyfill';
global['localStorage'] = localStorage;

// The Express app is exported so that it can be used by serverless Functions.
export function app() {
  const server = express();
  const distFolder = join(process.cwd(), 'dist/devfox/browser');
  const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : 'index';

  // Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
  server.engine('html', ngExpressEngine({
    bootstrap: AppServerModule,
  }));

  server.set('view engine', 'html');
  server.set('views', distFolder);

  // Example Express Rest API endpoints
  // app.get('/api/**', (req, res) => { });
  // Serve static files from /browser
  server.get('*.*', express.static(distFolder, {
    maxAge: '1y'
  }));

  // All regular routes use the Universal engine
  server.get('*', (req, res) => {
    res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
  });

  return server;
}

function run() {
  const port = process.env.PORT || 4000;

  // Start up the Node server
  const server = app();
  server.listen(port, () => {
    console.log(`Node Express server listening on http://localhost:${port}`);
  });
}

// Webpack will replace 'require' with '__webpack_require__'
// '__non_webpack_require__' is a proxy to Node 'require'
// The below code is to ensure that the server is run only when not requiring the bundle.
declare const __non_webpack_require__: NodeRequire;
const mainModule = __non_webpack_require__.main;
const moduleFilename = mainModule && mainModule.filename || '';
if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
  run();
}

export * from './src/main.server';

0 个答案:

没有答案