jollen.org

Jollen 的 Blog
Jollen's email: jollen # jollen.org
more:  Jollen's Training

如何自行架設 Parse API Server

.作者:jollen/
.日期:February 4, 2016 10:14 PM


Facebook 在 2016 年 1 月 28 日(台北時間 1 月 28 日)宣佈,將關閉 Parse.com 平台服務,但也同時釋出 Prase Platform 的 API Server 原始碼。本文介紹如何自行架設 Parse Server。

本文章採用 Markdown 語法撰寫(why?),若無法完整閱讀全文,請點擊這裡

前言

WoT.City 將在 2 月 4 日晩間 08:00PM ,舉辦 Parse.com API 伺服器的架設教學與討論交流。在家裡上網,跟著大家在 IRC 上一起架設 Parse 伺服器,共同迎接 IoT 個人架構時代的開始。

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 的免費資料庫服務(如圖一)。

2016-02-04 12 27 02 圖一:申請 MongoLab 服務

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 的 URI
  • cloud:Cloud Code 主程式
  • appId:Application ID

4.3 修改 databaseURI

如果是申請 MongoLab 的服務,請填入 MongoLab 提供的 URI 填入(如圖二)。<dbuser><dbpassword> 填入自行建立的 database user。

2016-02-04 12 35 16 圖二:申請的 MongoDB URI

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

Application ID 用來管理 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

Reports and Discussion

Tags:

純手工打造說明:技術專欄文章為 Jollen 原創,內容皆為人工撰寫,無 AI 生成。轉載請註明出處與作者,並全文引用。轉載時請在文章開頭或結尾明顯處註明「本文出處:https://www.jollen.org,已取得原作者同意並授權使用。」
訂閱電子報:不定期 Jollen's Blog 精選文章隨 Moko365 電子報寄送;請透過 Moko365 電子報訂閱(可隨時取消)。

Copyright(c) 2001–2016 www.jollen.org. All rights reserved.
Last update: 2026-07-22