如何使用 MongoDB 在 Heroku 上托管我自己的解析服务器? [英] How can I host my own Parse Server on Heroku using MongoDB?

查看:25
本文介绍了如何使用 MongoDB 在 Heroku 上托管我自己的解析服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(明确地说,我提出这个问题是为了提供我找到的答案,以帮助其他同样受到 Parse 关闭影响的人)


  1. 为您的解析服务器命名
  2. 暂时跳过应用的 APP_ID 和 MASTER_KEY,我们将在后面的步骤中进行设置.
  3. 点击免费部署"


  1. 如果出现以下框,请输入您的信用卡信息以验证您的帐户.别担心,除非您稍后升级帐户,否则我们不会向您收费.这只是 Heroku 为防止滥用而采取的一项安全措施.


  1. Heroku 将创建您的应用.在后台,它还会为您创建一个 .git repo 并克隆


    1. 填写您应用的 APP_ID 和 MASTER_KEY.如果您现在在 Parse.com 上有一个应用程序,您可以使用它使用的相同密钥.您可以在 https://www.parse.com/apps/<APP_NAME>/edit#keys 中找到,其中 是您的应用程序的名称.如果您要创建新应用,您可以


      1. 记下 MONGOLAB_URI 的值,如果您想将现有的 Parse 数据迁移到新数据库,稍后您将需要它
      2. 向下滚动并记下您应用的 Git Url 和 Heroku 域,稍后您将需要这些


      1. 安装

        注意:

        如果您将 jsfiddle 工具与多个解析服务器实例一起使用,您可能会收到错误会话令牌无效".如果发生这种情况,请打开开发控制台,并删除所有解析"文件.来自本地存储的密钥,之后,它应该可以工作:


        从 Parse 迁移现有数据

        在迁移生产应用之前,您应该至少对测试应用执行一次此操作.此外,您的旧应用程序中的旧 files 似乎尚未传输,请参阅


        1. 迁移完成后,点击Finalize";然后,在弹出窗口中单击好的"迁移完成后,您的所有数据都将位于新的 MongoDB 数据库中.


        1. 目前,您的 Parse 仪表板将继续显示您现在位于新数据库中的数据.要测试所有内容,请返回我们之前使用的测试 jsFiddle 并再次运行它.现在,转到您的 Parse 仪表板,您应该会看到新添加的类和行.注意,当我使用新的 Parse 仪表板时,我可以看到行数,但行区域全是空白的.我恢复到旧的 Parse 仪表板,可以看到一切正常.

        (To be clear, Im asking this question so as to provide the answer I found in an effort to help others that have been similarly affected by the Parse closure)

        Parse.com recently anounced that they are closing shop January 2017 but thankfully they have made their Parse Server software open source so we can all host our own Parse server instances.

        I have looked into the various options available for hosting Parse Server and have decided to host mine on Heroku

        I loved Parse because it was so easy to use and I have no real experience with setting up a backend. I have tried to follow several guides online but found them all a bit hard to understand with vague steps or steps that take you down a rabbit hole on another site installing a bunch of tools and it all gets a bit confusing.

        How can I host Parse Server on heroku, set it up to accept cross domain requests from my application, create a MongoDB database, and migrate all of my data from Parse to the new database?

        解决方案

        Self hosting Parse Server on Heroku


        Setup Heroku, MongoDB, and Parse

        1. Visit https://signup.heroku.com/ and sign up for an acount
        2. Verify your email, and log into Heroku
        3. Go to Deploying a Parse Server to Heroku
        4. Click "Deploy to Heroku"


        1. Give your parse server a name
        2. Skip the app's APP_ID and MASTER_KEY for now, we'll set that in later step.
        3. Click "Deploy for free"


        1. If the below box appears, enter your credit card info to verify your account. Dont worry, you wont be charged unless you upgrade your account later. This is simply a security measure Heroku has in place to prevent abuse.


        1. Heroku will create your app. In the background it will also create a .git repo for you and clone the contents of the official parse-server-example git repo, create a MongoDB database, and configure your Heroku app to use that DB.
        2. When the app is created, you'll be taken to it's Heroku dashboard or you can get there by logging in, clicking "Personal Apps" then clicking on the name of your parse server
        3. On your app's dashboard, click "Settings" then click "Reveal config vars"


        1. Fill in your app's APP_ID and MASTER_KEY. If you have an app on Parse.com now, you can use the same keys it uses. You can gey the at https://www.parse.com/apps/<APP_NAME>/edit#keys where <APP_NAME> is the name of your app. If you're creating a new app you can generate random keys here. You can add new keys for any sdks you plan to use, I'm adding one for the javascript sdk here.


        1. Make note of the value for MONGOLAB_URI, you'll need this later if you want to migrate your existing Parse data to your new DB
        2. Scroll down and make a note of the Git Url and the Heroku domain for your app, you'll need these later


        1. Install the Heroku Toolbelt which is a command line interface tool for managing your Heroku apps.

        2. Open a terminal prompt and enter $ heroku login, enter your Heroku email and password

        3. Enter $ heroku git:clone -a <MY_APP_NAME> (where <MY_APP_NAME> is the name of your heroku app) to make a local copy of your server code repo, you can also use the git url from we saw earlier. When you clone the repo you will see the message "warning: You appear to have cloned an empty repository.". The app folder will be created but it will only contain the standard git files and no parse server. To fix this. Go to the official parse-server-example git repo, download the zip file of the repo. Extract the folder, drag all of the files and folders from the parse-server-example-master folder into your app folder.

        4. Back in the terminal, enter $ cd <MY_APP_NAME>to move into the repo

        Enable CORS (so you can use the API from other domains)

        1. On your hard drive, find and open the local copy of the repo that we just created
        2. In the folder, open package.json and add "cors": "*" to the dependencies like this:

        .

          "dependencies": {
            "express": "~4.2.x",
            "kerberos": "~0.0.x",
            "parse": "~1.6.12",
            "parse-server": "~2.0",    // <= don't forget this comma
            "cors": "*"                // add this line
          }
        

        NOTE

        Make sure to remove the above comments in your actual code as it wont be valid JSON

        1. Open index.js and make these changes:

        .

        var express = require('express');  // find this line in the file
        var cors = require('cors') // add this line below it
        
        //....
        
        
        //var databaseUri = process.env.DATABASE_URI || process.env.MONGOLAB_URI // old name, you may need to change this for new deployments
        var databaseUri = process.env.DATABASE_URI || process.env.MONGODB_URI  // changed to MONGODB_URI in Heroku!
        
        //....
        
        var app = express();  // find this line in the file
        app.use(cors()); // add this line below it
        
        //Add declarations for any keys you plan to use as shown below
        var api = new ParseServer({
          databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
          cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
          appId: process.env.APP_ID || 'myAppId',
          masterKey: process.env.MASTER_KEY || '',
          serverURL: process.env.SERVER_URL || 'http://localhost:1337',
          javascriptKey: process.env.JAVASCRIPT_KEY || '',  //** add this line no need to set values, they will be overwritten by heroku config vars
          restAPIKey: process.env.REST_API_KEY || '', //** add this line
          dotNetKey: process.env.DOT_NET_KEY || '', //** add this line
          clientKey: process.env.CLIENT_KEY || '', //** add this line
        });
        

        1. Go back to the terminal window, make sure you are still in the folder of the repo we cloned and enter the below to push the changes to Heroku:

        .

        $ git add .
        $ git commit -am "make it better"
        $ git push heroku master
        


        Test your new Parse Server

        1. Go to this jsFiddle page
        2. Change <MY_APP_ID>,<MY_JS_KEY>,<MY_HEROKU_APP_NAME> in the fiddle to the appropriate values for your app then click "Run"

        .

        Parse.initialize('<MY_APP_ID>', '<MY_JS_KEY>');
        Parse.serverURL = 'https://<MY_HEROKU_APP_NAME>.herokuapp.com/Parse'
        

        1. You should get the below alert letting you know that your new Parse server is working correctly

        NOTE:

        If, you use the jsfiddle tool with multiple parse server instances, you might get the error "invalid session token". If this happens, open the dev console, and delete all of the "parse" keys from local storage, after that, it should work:


        Migrate your existing data from Parse

        You should do this at least once with a test app before migrating a production app. Also, it seems that legacy files from your old app may not yet transfer, see this GitHub Issue

        1. Before we migrate, if your new MongoDB has data in it the migration will fail. If you just tested the server and created and object, go to https://dashboard.heroku.com/apps/<MY_HEROKU_APP_NAME>/resources, click "MongoLab" next to it's icon, then, on the next page, click "Delete all collections"
        2. Go to https://dashboard.parse.com/apps/<APP_NAME>/settings/general where <APP_NAME> is the name of your parse app
        3. Scroll down on the page and click "Migrate"
        4. In the modal, in the grey area (that doesn't look much like an input...) enter the MONGOLAB_URI that we made note of earlier
        5. Click "Begin the migration"


        1. When the migration is done, click "Finalize" then, in the popup click "Okay" when the migration is final, all your data will be in your new MongoDB database.


        1. For now, your Parse dashboard will continue to show your data that now lives in the new DB. To test everything, go back to the test jsFiddle we used before and run it again. Now, go to your Parse dashboard and you should see the newly added class and row. Note that when I used the new Parse dashboard, I could see the number of rows but the row area was all blank. I reverted to the old Parse dashboard and could see everything fine.

        这篇关于如何使用 MongoDB 在 Heroku 上托管我自己的解析服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆