介绍
Parse 是一个移动后端作为服务平台,自 2013 年以来由 Facebook 拥有。2016 年 1 月,Parse 宣布(http://blog.parse.com/announcements/moving-on/)其托管服务将在 2017 年 1 月关闭。
为了帮助用户远离该服务,Parse发布了一个开源版本的后端,名为 Parse Server,可以部署到运行Node.js和MongoDB的环境。
本指南补充了官方文档,详细说明了如何在Ubuntu 14.04系统上安装Parse Server,例如DigitalOcean Droplet。它主要是为正在考虑迁移应用程序的Parse开发人员提供一个起点,并且应该与官方Parse Server Guide(https://parse.com/docs/server/guide)一起阅读。
前提条件
本指南假定您有一个干净的 Ubuntu 14.04 系统,配置了非根用户,具有sudo
权限用于管理任务,您可能希望查看 新 Ubuntu 14.04 服务器检查列表系列中的指南。
此外,您的系统还需要运行 MongoDB 实例,您可以通过 如何在 Ubuntu 14.04 上安装 MongoDB开始操作。MongoDB 也可以通过在创建时将 此脚本添加到其用户数据自动安装在新 Droplet 上。
一旦您的系统配置了sudo
用户和MongoDB,请返回本指南并继续。
步骤 1 安装 Node.js 和开发工具
首先,将当前的工作路径更改到您的sudo
用户的主目录:
1cd ~
NodeSource为 Debian 和 Ubuntu Node.js 包提供一个 Apt 存储库,我们将使用它来安装 Node.js。
1curl -sL https://deb.nodesource.com/setup_5.x -o nodesource_setup.sh
您可以通过打开nano
或您所选择的文本编辑器来查看本脚本的内容:
1nano ./nodesource_setup.sh
接下来,运行nodesource_setup.sh
。sudo
的-E
选项告诉它保存用户的环境变量,以便脚本可以访问它们:
1sudo -E bash ./nodesource_setup.sh
一旦脚本完成,NodeSource存储库应该在系统上可用。我们可以使用apt-get
来安装nodejs
包。我们还会安装build-essential
元包,它提供了一系列可能在以后有用的开发工具,以及Git版本控制系统来从GitHub中获取项目:
1sudo apt-get install -y nodejs build-essential git
步骤 2 — 安装 Example Parse Server 应用程序
Parse Server 旨在与 Node.js 的一种流行的 Web 应用框架 Express 一起使用,该框架允许符合定义的 API 的中间软件组件在给定路径上安装。
以git
获取存储库:
1git clone https://github.com/ParsePlatform/parse-server-example.git
输入你刚刚克隆的parse-server-example
目录:
1cd ~/parse-server-example
使用npm
在当前目录中安装依赖,包括parse-server
:
1npm install
npm
将收集所有由parse-server
要求的模块,并将它们存储在~/parse-server-example/node_modules
中。
步骤 3 – 测试样本应用程序
使用「npm」來啟動服務,這會執行在「package.json」的「start」屬性中定義的命令,在這種情況下,它執行「node index.js」:
1npm start
1[secondary_label Output]
2> [email protected] start /home/sammy/parse-server-example
3> node index.js
4
5DATABASE_URI not specified, falling back to localhost.
6parse-server-example running on port 1337.
您可以随时通过按 Ctrl-C来终止运行应用程序。
在index.js中定义的Express应用将HTTP请求传递到parse-server
模块中,该模块会与您的MongoDB实例进行通信,并调用在~/parse-server-example/cloud/main.js
中定义的函数。
在这种情况下,Parse Server API 的终端点会调用默认值为:
「http://your_server_IP/分類」
在另一台终端中,您可以使用curl
来测试此终端点,请确保您先登录到您的服务器,因为这些命令引用localhost
而不是特定IP地址。
创建一个记录,通过发送一个POST
请求,用一个X-Parse-Application-Id
标题来识别应用程序,以及一些格式化为JSON的数据:
1curl -X POST \
2 -H "X-Parse-Application-Id: myAppId" \
3 -H "Content-Type: application/json" \
4 -d '{"score":1337,"playerName":"Sammy","cheatMode":false}' \
5 http://localhost:1337/parse/classes/GameScore
1[secondary_label Output]
2{"objectId":"fu7t4oWLuW","createdAt":"2016-02-02T18:43:00.659Z"}
您发送的数据存储在 MongoDB 中,可以通过使用弯曲
来发送一个GET
请求来获取:
1curl -H "X-Parse-Application-Id: myAppId" http://localhost:1337/parse/classes/GameScore
1[secondary_label Output]
2{"results":[{"objectId":"GWuEydYCcd","score":1337,"playerName":"Sammy","cheatMode":false,"updatedAt":"2016-02-02T04:04:29.497Z","createdAt":"2016-02-02T04:04:29.497Z"}]}
运行在 `~/parse-server-example/cloud/main.js 中定义的函数:
1curl -X POST \
2 -H "X-Parse-Application-Id: myAppId" \
3 -H "Content-Type: application/json" \
4 -d '{}' \
5 http://localhost:1337/parse/functions/hello
1[secondary_label Output]
2{"result":"Hi"}
步骤 4 – 配置样本应用程序
在原始终端中,按 Ctrl-C 以阻止 Parse Server 应用程序的运行版本。
如上所述,样本脚本可以通过使用六个 环境变量进行配置:
Variable | Description |
---|---|
DATABASE_URI | A MongoDB connection URI, like mongodb://localhost:27017/dev |
CLOUD_CODE_MAIN | A path to a file containing Parse Cloud Code functions, like cloud/main.js |
APP_ID | A string identifier for your app, like myAppId |
MASTER_KEY | A secret master key which allows you to bypass all of the app's security mechanisms |
PARSE_MOUNT | The path where the Parse Server API should be served, like /parse |
PORT | The port the app should listen on, like 1337 |
您可以在使用导出
命令运行脚本之前设置任何这些值,例如:
1export APP_ID=fooApp
它值得阅读index.js
的内容,但为了获得更清晰的图像发生了什么,你也可以写自己的更短的版本的例子。
1nano my_app.js
然后粘贴以下内容,在所需的地方更改突出值:
1[label ~/parse-server-example/my_app.js]
2var express = require('express');
3var ParseServer = require('parse-server').ParseServer;
4
5// Configure the Parse API
6var api = new ParseServer({
7 databaseURI: 'mongodb://localhost:27017/dev',
8 cloud: __dirname + '/cloud/main.js',
9 appId: 'myOtherAppId',
10 masterKey: 'myMasterKey'
11});
12
13var app = express();
14
15// Serve the Parse API on the /parse URL prefix
16app.use('/myparseapp', api);
17
18// Listen for connections on port 1337
19var port = 9999;
20app.listen(port, function() {
21 console.log('parse-server-example running on port ' + port + '.');
22});
退出并保存文件,然后使用 Node.js 运行它:
1node my_app.js
1[secondary_label Output]
2parse-server-example running on port 9999.
如上所述,样本my_app.js
将与所提供的index.js
几乎相同地行为,但它将听到端口9999,Parse Server 安装在/myparseapp
,因此终端 URL 看起来像这样:
您的服务器: IP:9999/myparseapp
它可以用curl
来测试,如下:
1curl -H "X-Parse-Application-Id: myOtherAppId" http://localhost:9999/myparseapp/classes/GameScore`
结论
你现在应该知道在Ubuntu环境中运行Node.js应用程序(如Parse Server)的基本知识,完全从Parse迁移应用程序很可能是一个更具参与性的承诺,需要代码更改和基础设施的仔细规划。
有关这个过程的更多细节,请参阅本系列的第二个指南, 如何将一个 Parse 应用程序迁移到 Ubuntu 14.04 上的 Parse 服务器.您还应该参考官方 Parse 服务器指南,特别是关于 迁移现有 Parse 应用程序的部分。