美文网首页技术流
从零开始使用ParseServer

从零开始使用ParseServer

作者: 每天多一点 | 来源:发表于2017-06-22 17:42 被阅读2435次

    安装

    假设我们有一台干净的Linux机器, 这里我们使用ubuntu.

    • 需要确认一下npm已经安装
    apt-get install npm
    

    切换源

    npm config set registry http://registry.cnpmjs.org
    

    注意有时候我们会遇到

    /usr/bin/env: ‘node’: No such file or directory
    

    这个问题是因为Ubuntu下node的运行命令是nodejs, 所以我们需要创建一个软连接

    ln -s /usr/bin/nodejs /usr/bin/node
    
    apt-get install mongodb
    

    测试Mongodb

    lsof -iTCP:27017 -sTCP:LISTEN
    
    • 创建config文件, 我们新建一个文件config.json:
    {
      "appId": "appid1",
      "masterKey": "mk1",
      "appName": "test1",
      "cloud": "./cloud/main",
      "databaseURI": "mongodb://127.0.0.1:27017/parse"
    }
    
    • 运行如下命令:
    parse-server config.json
    

    可以得到:

    Configuration loaded from /root/parse/config.json
    appId: appid1
    masterKey: ***REDACTED***
    port: 1337
    host: 0.0.0.0
    databaseURI: mongodb://127.0.0.1:27017/parse
    cloud: ./cloud/main
    mountPath: /parse
    appName: test1
    maxUploadSize: 20mb
    userSensitiveFields: ["email"]
    serverURL: http://localhost:1337/parse
    
    [10705] parse-server running on http://localhost:1337/parse
    
    • 测试
    curl -X POST -H "X-Parse-Application-Id:appid1" \
    -H "Content-Type: application/json" \
    -d '{"score":1337,"playerName":"Sean Plott","cheatMode":false}' \
    http://127.0.0.1:1337/parse/classes/GameScore
    

    Dashboard

    参考 https://github.com/parse-community/parse-dashboard

    • 创建config文件dashboard-config.jsons
    {
      "apps": [
        {
          "serverURL": "http://localhost:1337/parse",
          "appId": "myAppId",
          "masterKey": "myMasterKey",
          "appName": "MyApp"
        }
      ]
    }
    
    • 运行
    parse-dashboard --config dashboard-config.json
    

    HttpsNginx

    需要安装nginx

    apt-get install nginx
    
    server { 
     listen 443 ssl default_server;
     listen [::]:443 ssl default_server;
     ###其他配置 ssl on;  
     ssl_certificate      /var/www/ssl/server.crt; 
     ssl_certificate_key  /var/www/ssl/server_nopwd.key; 
    
     location /parse {   
        proxy_pass http://127.0.0.1:1337;         
        proxy_set_header Host $host;  
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    
      }
    …
    
    • 再次测试, 注意将ip替换成自己部署的服务
    curl -k -X POST -H "X-Parse-Application-Id:appid1”
    -H "Content-Type: application/json" \
    -d '{"score":1337,"playerName":"Sean Plott","cheatMode":false}' \
    https://<ip>/parse/classes/GameScore
    

    Dashboard支持https

    如果已经将parse server部署到了一个https服务上,dashboard的配置中的链接要更改为相应的链接
    同时,dashboard本身需要配置trustProxy参数和users

    使用Supervisor守护

    Supervisor是一个Python写的工具, 用来帮助我们管理多个进程.
    首先使用Supervisor的命令创建一个配置文件,

    echo_supervisord_conf > my_supervisord.conf
    

    修改这个配置文件, 具体可以参考官网的说明, 只需要加入其中的三行就可以了.

    [program:parseserver]
    command=parse-server config.json
    process_name="ParseServer"
    

    注意:如果需要使用web/socket进行远程管理, 还需要配置一下其他项目.

    运行

    supervisord -c supervisord.conf
    

    移动端使用

    carthage update
    

    后, 需要拖入两个库:
    Parse.framework和Bolts.framework

    相关文章

      网友评论

      • 4ba977717ba4:对于新手来说两个细节需要改一下。
        --> "cloud": "./cloud/main",
        这一行去掉,不然出错,因为说明中并没有创建这个文件。

        --> 创建config文件dashboard-config.jsons
        多了一个s
        每天多一点:谢谢, 按照你的提示进行了修改.

      本文标题:从零开始使用ParseServer

      本文链接:https://www.haomeiwen.com/subject/ffyfcxtx.html