如何自行架設 Parse Server
Closed this issue · 5 comments
已發佈至 Jollen's Blog
前言
parse-server 是一個相容於 Express 的 URL Router 套件,這表示:
- 以 npm 模組方式引入使用,不需要直接 fork 此專案
- 可以建立新的 Express application 專案來使用
- 或是,將 parse-server 掛載(mount)到現有的 Express application 專案上使用
以下從建立新的 Express application 專案開始,介紹 parse-server URL router 套件的使用方法。
如果不想自行使用 Express Generator 產生專案的話,請參考 nodejs-express 並直接由 Step 4 開始。
本文目標:
- 前置作業
- 建立 Node.js 與 Express 專案
- Create the first ParseServer instance and Cloud Code
- Use restAPIKey
- 測試 REST API
前置作業
環境安裝:
- 安裝 Node.js 4.1 以上版本(建議 v4.2.x TLS)
- 安裝 MongoDB Server,或直接到 MongoLab 申請免費的 MongoDB 資料庫服務即可
MongoDB 的新手建議可先申請 MongoLab 服務。MongoLab 目錄提供 500MB 的免費資料庫服務(如圖一)。
Step 1: 安裝 Express Generator
$ sudo npm install express-generator -g
Step 2: 建立新的 Express 專案
$ express my-parse-app
create : my-parse-app
create : my-parse-app/package.json
create : my-parse-app/app.js
create : my-parse-app/public
create : my-parse-app/public/javascripts
create : my-parse-app/public/images
create : my-parse-app/public/stylesheets
create : my-parse-app/public/stylesheets/style.css
create : my-parse-app/routes
create : my-parse-app/routes/index.js
create : my-parse-app/routes/users.js
create : my-parse-app/views
create : my-parse-app/views/index.jade
create : my-parse-app/views/layout.jade
create : my-parse-app/views/error.jade
create : my-parse-app/bin
create : my-parse-app/bin/www
install dependencies:
$ cd my-parse-app && npm install
run the app:
$ DEBUG=my-parse-app:* npm start
進入專案目錄,並安裝 npm 模組:
$ cd my-parse-app/
$ npm install
目前的專案內容:
$ ls -l
total 16
-rw-r--r-- 1 apple staff 1442 2 3 14:22 app.js
drwxr-xr-x 3 apple staff 102 2 3 14:22 bin
drwxr-xr-x 3 apple staff 102 2 3 14:23 node_modules
-rw-r--r-- 1 apple staff 362 2 3 14:23 package.json
drwxr-xr-x 5 apple staff 170 2 3 14:22 public
drwxr-xr-x 4 apple staff 136 2 3 14:22 routes
drwxr-xr-x 5 apple staff 170 2 3 14:22 views
Step 3: 安裝 parse-server 模組
$ npm i parse-server --save
Step 4: 修改 app.js 主程式
開啟 app.js 主程式,分別加入以下幾段程式碼。
4.1. 引入 parse-server 模組:
var ParseServer = require('parse-server').ParseServer;
4.2. 建立 ParseServer 的 instance
修改 app.js
,加入:
// Specify the connection string for your mongodb database
// and the location to your Parse cloud code
var api = new ParseServer({
databaseURI: 'mongodb://localhost:27017/dev',
cloud: '/home/myApp/cloud/main.js', // Provide an absolute path
appId: 'myAppId',
masterKey: 'mySecretMasterKey',
fileKey: 'optionalFileKey'
});
以上有幾個選項要修改:
databaseURI
:MongoDB 的 URIcloud
:Cloud Code 主程式appId
:Application ID
4.3 修改 databaseURI
如果是申請 MongoLab 的服務,請填入 MongoLab 提供的 URI 填入(如圖二)。<dbuser>
與 <dbpassword>
填入自行建立的 database user。
4.4 加入 Cloud Code
這個是 Parse Cloud Code 的程式碼路徑,請建立想存放 Parse Cloud Code 的路徑,接著建立 main.js 主程式,內容如下:
Parse.Cloud.define("hello", function(request, response) {
response.success('hello');
});
4.5 設定 appId
appId
用來管理 Parse API 的使用權限,原則上指定一個字串,例如:5de49f1cd4bd09be95bf35ecbf1117b0
。
Mac / Linux 的使用者,可以用 md5
做一個 md5sum 當 appId 用:
$ md5 /etc/hosts
MD5 (/etc/hosts) = 91df01d82a846dbddc163a65c0f7b047
4.6 掛載 parse-server 的 URL router
修改 app.js
加入:
// Serve the Parse API on the /parse URL prefix
app.use('/parse', api);
4.6 練習加入 restAPIKey
同樣的觀念,為 Parse Server 加入 restAPIKey
。修改示範:
完整示範
提供 app.js 程式碼修改示範:
Step 5: 啟動 MongoDB
申請 MongoLab 服務請略過本步驟。
Step 6: 啟動 Node.js
$ npm start
API 測試
使用 curl
來呼叫 Parse 的 REST API 進行初步測試。閱讀 REST API Guide 了解 Parse Server API 細節。
Creating Objects
測試指令:
curl -X POST \
-H "X-Parse-Application-Id: 123456789" \
-H "X-Parse-REST-API-Key: 9d676c364a11d96b8c67e69bf7bbfb82" \
-H "Content-Type: application/json" \
-d '{"score":1337,"playerName":"Sean Plott","cheatMode":false}' \
http://localhost:3000/parse/classes/GameScore
返回結果:
{"objectId":"B2BjW12yXo","createdAt":"2016-02-04T05:29:35.187Z"}
Parse API 使用 X-Parse-Application-Id
檔頭送出 appId
,請填入自行設定的 appId
。
參考資源
- Parse Server Guide, https://parse.com/docs/server/guide
Jollen你好 請問一下,如果我直接fork這個專案來push到heroku上面的話也是可行的嗎? 我fork整個專案下來後在./src/index.js裡面把appId, masterKey, serverURL, 改成我的部分後其他沒有變動,然後push 到heroku上面,It's not work to access my db. 請問我有什麼是沒注意到的嗎? 或者必須要再開一個express app才是對的呢?
最好直接 fork 官方的 repo,佈署到 heroku 我沒試過,但記得要設定好 MongoDB URI。
謝謝~ 另外想問,直接fork專案下來的話是否就要定期pull新的官方repo下來做更新囉? 如果我用express app 引入 parse-server 模組的話是不是就不用了(因為require('parse-server').ParseServer;都是新的模組),不知我的觀念是否正確? 謝謝你~
用 Express 去 mount 的話,用 npm 就可以升級了。以 Standalone 方式,又改了 Parse 的程式碼,就可以順便練 git flow 了。
非常感謝你!