简体   繁体   English

在Heroku上部署Ionic2

[英]Deploying Ionic2 on Heroku

I am trying to deploy my ionic2 app on Heroku. 我正在尝试在Heroku上部署我的ionic2应用程序。 I looked at these sites: 我看了看这些网站:

and created a server.js file: 并创建了一个server.js文件:

var express  = require('express');
var app      = express();                               // create our app w/ express
var morgan = require('morgan');             // log requests to the console (express4)
var bodyParser = require('body-parser');    // pull information from HTML POST (express4)
var cors = require('cors');

app.use(morgan('dev'));                                         // log every request to the console
app.use(bodyParser.urlencoded({'extended':'true'}));            // parse application/x-www-form-urlencoded
app.use(bodyParser.json());                                     // parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json
// app.use(methodOverride());
app.use(cors());

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header('Access-Control-Allow-Methods', 'DELETE, PUT');
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

app.use(express.static('www'));
app.set('port', process.env.PORT || 5000);
app.listen(app.get('port'), function () {
  console.log('Express server listening on port ' + app.get('port'));
});

Since I have no models and no DB at this time, I excluded mongo and mongoose. 由于我现在没有模型也没有数据库,所以我排除了mongo和mongoose。

The server runs fine when I run it on my local machine via npm start but when I run it on heroku, I get: 当我通过npm start在我的本地机器上运行它时,服务器运行正常但是当我在heroku上运行它时,我得到:

Cannot GET /

on the page. 在页面上。 Heroku Logs shows: Heroku日志显示:

2017-01-04T19:56:59.385666+00:00 heroku[web.1]: State changed from starting to up
2017-01-04T19:57:00.546815+00:00 heroku[router]: at=info method=GET path="/" host=hrmghp-companion.herokuapp.com request_id=4c010120-3dce-4f99-b31c-99dc0883f314 fwd="108.44.230.178" dyno=web.1 connect=1ms service=49ms status=404 bytes=364
2017-01-04T19:57:00.549928+00:00 app[web.1]: GET / 404 19.924 ms - 13

Am I missing something in my server.js file? 我在server.js文件中遗漏了什么?

Edit: I found the issue. 编辑:我发现了这个问题。 I had www/ in my .gitignore file. 我在.gitignore文件中有www/ I assumed that it would rebuild the app when deploying to heroku? 我假设在部署到heroku时会重建应用程序? Is this not how it works? 这不是它的工作原理吗?

Don't add www to your repository. 不要将www添加到您的存储库。 You don't want to keep track of all those files. 您不想跟踪所有这些文件。 Instead, include "postinstall": "ionic-app-scripts build" in the scripts section of your package.json. 相反,在package.json的scripts部分中包含"postinstall": "ionic-app-scripts build" This will rebuild the app and regenerate the www folder for you on Heroku. 这将重建应用程序并在Heroku上为您重新生成www文件夹。

Just want to add something on to Alex's answer. 只想在Alex的回答中加入一些内容。 Make sure you either add ionic-app-scripts as a dependency (as opposed to devDependency), OR disable production mode in heroku via heroku config:set NPM_CONFIG_PRODUCTION=false 确保将ionic-app-scripts添加为依赖项(而不是devDependency),或者通过heroku config:set NPM_CONFIG_PRODUCTION=false禁用heroku中的生产模式heroku config:set NPM_CONFIG_PRODUCTION=false

So, adding "postinstall": "ionic-app-scripts build" in the scripts section of package.json, and disabling production mode worked for me. 所以,在package.json的scripts部分添加"postinstall": "ionic-app-scripts build" ,禁用生产模式对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM