【问题标题】:Angular 2 Universal Heroku DeployAngular 2 通用 Heroku 部署
【发布时间】:2016-11-26 22:57:48
【问题描述】:

我一直在关注使用 Angular 2 Universal 实现应用程序的教程。在heroku上我得到了

 at=error code=H10 desc="App crashed" method=GET path="/"

和 server.ts 是

// the polyfills must be the first thing imported in node.js
import 'angular2-universal/polyfills';

import * as path from 'path';
import * as express from 'express';
import * as bodyParser from 'body-parser';
import * as cookieParser from 'cookie-parser';

// Angular 2
import { enableProdMode } from '@angular/core';
// Angular 2 Universal
import { expressEngine } from 'angular2-universal';

// enable prod for faster renders
enableProdMode();

const app = express();
const ROOT = path.join(path.resolve(__dirname, '..'));

// Express View
app.engine('.html', expressEngine);
app.set('views', __dirname);
app.set('view engine', 'html');

app.use(cookieParser('Angular 2 Universal'));
app.use(bodyParser.json());

// Serve static files
app.use('/assets', express.static(path.join(__dirname, 'assets'), {maxAge: 30}));
app.use(express.static(path.join(ROOT, 'dist/client'), {index: false}));


import { serverApi } from './backend/api';
// Our API for demos only
app.get('/data.json', serverApi);

import { ngApp } from './main.node';
// Routes with html5pushstate
// ensure routes match client-side-app
app.get('/', ngApp);
app.get('/about', ngApp);
app.get('/about/*', ngApp);
app.get('/home', ngApp);
app.get('/home/*', ngApp);
app.get('/ourwork', ngApp);
app.get('/ourwork/*', ngApp);
app.get('/services', ngApp);
app.get('/services/*', ngApp);
app.get('/portfolio', ngApp);
app.get('/portfolio/*', ngApp);
app.get('/contact', ngApp);
app.get('/contact/*', ngApp);

// use indexFile over ngApp only when there is too much load on the server
function indexFile(req, res) {
  // when there is too much load on the server just send
  // the index.html without prerendering for client-only
  res.sendFile('/index.html', {root: __dirname});
}

app.get('*', function(req, res) {
  res.setHeader('Content-Type', 'application/json');
  var pojo = { status: 404, message: 'No Content' };
  var json = JSON.stringify(pojo, null, 2);
  res.status(404).send(json);
});

// Server
app.listen(3000, () => {
  console.log('Listening on: http://localhost:3000');
});

我一直在环顾四周,找不到这个问题的答案。任何帮助将不胜感激。

【问题讨论】:

    标签: express heroku angular seo


    【解决方案1】:

    尝试使用:

    // Server
    app.listen((process.env.PORT || 3000), () => {
      console.log('Listening on port %d', this.address().port);
    });
    

    而不是

    // Server
    app.listen(3000, () => {
      console.log('Listening on: http://localhost:3000');
    });
    

    使用 heroku 提供的端口而不是硬编码 3000。

    【讨论】:

    • 感谢您的回复,不幸的是,当我添加这行代码时,我开始在 Heroku 上收到以下错误。在 webpack_require (/app/dist/server/index.js:21:30),在 Object. (/app/dist/server/index.js:53:21),在webpack_require (/app/dist/server/index.js:21:30) 你知道我如何修复 Angular 2 Universal 启动包中的这些错误吗?
    • 我不太了解您评论中的新错误。您可以将它们添加到您的问题中吗?
    • 请忽略此评论 ^^ 我发现解决方案是我的开发依赖项与我的产品依赖项不同。谢谢
    • 支票在我这边显示为绿色,是不是对你显示?是的,先生,谢谢你,它确实有帮助。
    • 确实如此,很高兴我能帮上忙!
    猜你喜欢
    • 2017-12-03
    • 2017-05-14
    • 1970-01-01
    • 1970-01-01
    • 2017-11-26
    • 2017-01-31
    • 2017-11-25
    • 2023-04-05
    • 1970-01-01
    相关资源
    最近更新 更多