Heroku に node.js の環境を作ってデプロイしてみることにしました。
↓このサイトの説明がすごくわかりやすかったです。
しかし、説明通りにして、デプロイして開くと、heroku でエラー表示になってしまいました。
そこで、heroku logs -t で、ログを出力すると、
Starting process with command
npm start
とエラーが書いてありました。
調べてみると、package.json の中に書いてある、↓こちらの部分を
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
↓↓↓↓↓
"scripts": {
"start": "node index",
"test": "echo \"Error: no test specified\" && exit 1"
},
このように、「start」と書かかれている1行を追加してあげないとダメなようです。
修正して、heroku open のコマンドを打つと、
↓こちらのURLがブラウザで開き、
https://sheltered-crag-XXXXX/herokuapp.com/
「hello world index.」と表示されました。
その時の index.js のコードが、↓こちら。
var express = require('express');
var http = require('http');
var app = express();
app.set('x-powered-by', false);
app.set('case sensitive routing', true);
app.set('strict routing', true);
app.use(function (req, res, next) {
res.status(200);
res.type('text/plain; charset=utf-8');
next();
});
app.get('/', function (req, res, next) {
res.send('hello world index.\r\n');
});
app.get('', function (req, res, next) {
res.status(404);
res.send(http.STATUS_CODES[404] + '\r\n');
});
app.all('', function (req, res, next) {
res.status(501);
res.send(http.STATUS_CODES[501] + '\r\n');
});
var server = app.listen(process.env.PORT, function () {
console.log('http server is running...');
var f = false;
process.on('SIGTERM', () => {
if (f) {
return;
}
f = true;
console.log('http server is closing...');
server.close(function () {
console.log('http server closed.');
process.exit(0);
});
});
});
server.on('error', function (err) {
console.error(err);
process.exit(1);
});