標簽:NodeJS
,Heroku
0
這里是這個偽系列的第一篇,因為我也不知道自己能不能做完,然后到底能做成什么樣子。總之,盡力而為吧,加油。
1 Heroku App 的構成
Heroku 所謂的 NodeJS App ,看起來像一個npm模塊,需要一個package.json,語法也跟npm模塊的語法幾乎一樣。
需要在里面寫明運行的Node版本及npm版本,以及需要的其他模塊依賴及版本。
必須的幾個文件:
- package.json //定義引擎及依賴
- Procfile //告訴Heroku,App所需要的權限(網絡什么的),運行什么命令啟動你的App
- server.js //總得有個JS文件才能跑吧,不過不限制名字
package.json
:
{
"name": "blog-node",
"version": "1.0.0",
"description": "rocka's node blog",
"engines": {
"node": "6.3.1",
"npm": "3.10.3"
},
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Rocket1184/NodeServerTest.git"
},
"author": "r0cka",
"license": "MIT"
}
Procfile
:
web: node server.js
1 開始寫服務器
我對這個服務器沒什么大要求,只要能返回個404頁面就行。相當於一個HelloWorld。
所以我們先需要一個404頁面,404.html
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Error: Not Found</title>
<style>
h1,
p {
text-align: center;
}
</style>
</head>
<body>
<h1>Error: 404 Not Found</h1>
<p>File not exist, please check your url.</p>
<hr>
<p>Node.js - v6.3.1</p>
</body>
</html>
沒啥好說的,一個隨隨便便的404就好。
然后開始接收請求,准備返回404頁面了~~~
這里貼上我的 server.js
:
'use strict';
var http = require('http');
var fs = require('fs');
var server = http.createServer((request, response) => {
console.log(request.method + ': ' + request.url);
if (request.method === 'GET') {
if (request.url === '/favicon.ico') {
fs.createReadStream('./favicon.ico').pipe(response);
} else {
response.writeHead(200, { 'Content-Type': 'text/html' });
fs.createReadStream('./404.html').pipe(response);
}
}
});
var serverPort = process.env.PORT || 5000;
server.listen(serverPort);
console.log(`[Rocka Node Server] Running at http://127.0.0.1:${serverPort}/`);
用自帶的http API創建服務器,處理請求:
如果是GET請求,而且想要拿到 /favicon.ico
,那么用 fs
模塊把文件讀出來,通過管道傳給響應。
否則讀取 404.html
,把他傳出去。
值得注意的一點是,Heroku會為你的App動態分配,端口。如果把服務器監聽的端口寫死
server.listen(8080);
構建成功,但幾乎肯定會出現這個錯誤:
Web process failed to bind to $PORT within 60 seconds of launch
所以,把端口的值改成Heroku API提供的動態值 process.env.PORT
就好了~~
2 package.json 的一個坑
因為缺乏對npm的了解,我在這一步犯了一個看起來非常小白的錯誤:
由於我在使用node寫服務器時,引用了node自帶的fs
和http
模塊,就以為寫依賴的時候要把這些也寫進去。於是Heroku構建時就各種報錯,構建失敗:
npm ERR! Linux 3.13.0-91-generic
npm ERR! argv "/tmp/build_7f369635973e963166afa93b918df0b4/Rocket1184-NodeServerTest-8575934/.heroku/node/bin/node" "/tmp/build_7f369635973e963166afa93b918df0b4/Rocket1184-NodeServerTest-8575934/.heroku/node/bin/npm" "install" "--unsafe-perm" "--userconfig" "/tmp/build_7f369635973e963166afa93b918df0b4/Rocket1184-NodeServerTest-8575934/.npmrc"
npm ERR! node v6.3.1
npm ERR! npm v3.10.3
npm ERR! No compatible version found: fs@3.10.3
npm ERR! Valid install targets:
npm ERR! 0.0.2, 0.0.0
npm ERR!
npm ERR!
npm ERR! If you need help, you may report this error at:
npm ERR! <https://github.com/npm/npm/issues>
npm ERR! Please include the following file with any support request:
npm ERR! /tmp/build_7f369635973e963166afa93b918df0b4/Rocket1184-NodeServerTest-8575934/npm-debug.log
起初我以為是自己的版本寫的不對,就一再的修改fs
和http
的版本號,從3.10.3
到3.x
再到>=0.0.1
,依舊構建失敗。
一直構建不過,肯定是哪里出了問題。於是來看看自己本地的這些模塊的版本號:
$ npm ls
node-server-test@1.0.0 D:\Documents\GitHub\NodeServerTest
`-- (empty)
這里面的模塊是空的!!!
廢話,什么都沒裝,當然是空的。
我立馬在package.json
里面注釋掉了這些東西(VSCode里面瀟灑的一個Ctrl+/):
// "dependencies": {
// "fs": "3.x",
// "http": "3.x"
// },
好了,commit
,push
,deploy
,納尼!!居然又報錯了!!!
-----> Node.js app detected
parse error: Invalid numeric literal at line 12, column 5
! Unable to parse package.json
-----> Build failed
啊,JSON里面不能加注釋啊。。。。好了,刪掉,重跑,總算成功了。
倉庫地址
GitHub倉庫:BlogNode
主倉庫,以后的代碼都在這里更新。
HerokuApp:rocka-blog-node
上面GitHub倉庫的實時構建結果。