找到一个非常不错的工具, Parse-Server, 用它可以像 Firebase 那样提供 APP 后台, 且可以动态添加 JSON 数据进去, 并指定路径. 故在 APP 开发过程中十分有用.

Parse-Server 是一款开源的后端模拟工具, 且只要能安装 Node 的平台都可以安装它.

使用也非常简单, 详细的使用方法可以参考它的 Github 主页或它的文档.

另外它还提供了针对若干平台的 Parse SDK, 这样可以在程序中实现增删改查数据, iOS 端的 SDK 文档在这里, API 文档在这里.

关于 Parse-Server 的完整 REST API 文档, 在这里可以找到.

下面就来看它的简单安装和使用.

安装和使用

  1. 安装 node: 这个网上很多教程…

  2. 安装并启动 Parse-Server:

bash
npm install -g parse-server mongodb-runner
mongodb-runner start
parse-server --appId APPLICATION_ID --masterKey MASTER_KEY --databaseURI mongodb://localhost/test
  1. 添加数据(这里添加的是对象):
bash
curl -X POST \
-H "X-Parse-Application-Id: APPLICATION_ID" \
-H "Content-Type: application/json" \
-d '{"score":1337,"playerName":"Sean Plott","cheatMode":false}' \
http://localhost:1337/parse/classes/GameScore

添加对象后会收到对象对应的 ID:

bash
{
  "objectId": "2ntvSpRGIK",
  "createdAt": "2016-03-11T23:51:48.050Z"
}
  1. 获取之前添加的对象:
bash
$ curl -X GET \
-H "X-Parse-Application-Id: APPLICATION_ID" \
http://localhost:1337/parse/classes/GameScore/2ntvSpRGIK

其中 2ntvSpRGIK 即创建对象时候的对象 ID.

收到的响应如下:

bash
{
  "objectId": "2ntvSpRGIK",
  "score": 1337,
  "playerName": "Sean Plott",
  "cheatMode": false,
  "updatedAt": "2016-03-11T23:51:48.050Z",
  "createdAt": "2016-03-11T23:51:48.050Z"
}

如果要获取对象接口下的所有对象, 则可以这样:

bash
curl -X GET \
  -H "X-Parse-Application-Id: APPLICATION_ID" \
  http://localhost:1337/parse/classes/GameScore

响应如下:

bash
{
  "results": [
    {
      "objectId": "2ntvSpRGIK",
      "score": 1337,
      "playerName": "Sean Plott",
      "cheatMode": false,
      "updatedAt": "2016-03-11T23:51:48.050Z",
      "createdAt": "2016-03-11T23:51:48.050Z"
    }
  ]
}
  1. 另外有 DashBoard 可用:

安装方法如下:

bash
npm install -g parse-dashboard

启动:

bash
parse-dashboard --appId mytestapp --masterKey abc123 --serverURL "http://localhost:1337/parse" --appName TestApp

默认情况下这个面板不支持 HTTP 协议下的远程访问(部署在本地可用使用 http://localhost), 故这里需要配置 nginx 来支持 HTTPS 下访问其他端口…

远程部署的需求暂时没有, 可以先部署到本地直接使用.

附1: Nginx 配置非 433 端口的 HTTPS 访问

bash
location /pss {
  proxy_pass   http://localhost:1337/parse;
  proxy_redirect off;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

附2: Linux 根据进程名称查找进程

有下面这三种方法:

  • pidof firefox

  • ps aux | grep -i firefox

  • pgrep firefox