Parse is a mobile back-end as a service platform, owned by Facebook since 2013. In January 2016, Parse announced that its hosting service will be closed in January 2017.
In order to help its users migrate from the service, Parse has released an open source version of the backend called Parse Server, which can be deployed to environments running Node.js and MongoDB.
This guide supplements the official documentation, which contains detailed instructions for installing Parse Server on Ubuntu 14.04, such as Tencent Cloud CVM. It first serves as a starting point for Parse developers who are considering migrating their applications, and should be read together with the official [Parse Server Guide] (https://parse.com/docs/server/guide).
An Ubuntu** server** with a non-root account that can use the sudo
command has been set up, and the firewall has been turned on. Those who don’t have a server can buy it from here, but I personally recommend you to use the free Tencent Cloud Developer Lab for experimentation, and then buy server.
In addition, your system will need a running MongoDB instance. By adding this script to its user data when creating a Droplet, MongoDB can also be automatically installed on the new CVM.
After configuring the system with the sudo
user and MongoDB, please return to this guide and continue.
First change the current working path to the home directory of the sudo
user:
cd ~
NodeSource provides an Apt repository for Debian and Ubuntu Node.js packages. We will use it to install Node.js. NodeSource provides an installation script for the latest stable version (v5.5.0 at the time of writing), which can be found in Installation Instructions. Download the script curl
:
curl -sL https://deb.nodesource.com/setup_5.x -o nodesource_setup.sh
You can open the contents of the script using nano
or a text editor of your choice:
nano ./nodesource_setup.sh
Next, run nodesource_setup.sh
. Tell it to keep the user's environment variables so that the script can access them with the -E
option sudo
:
sudo -E bash ./nodesource_setup.sh
After the script completes, the NodeSource repository should be available on the system. We can use apt-get
to install the nodejs
package. We will also install the build-essential
metapackage, which provides a series of development tools that may be useful in the future, and the Git version control system for retrieving projects from GitHub:
sudo apt-get install -y nodejs build-essential git
Parse Server is designed to be used in conjunction with Express, a popular web application framework for Node.js, which allows middleware components conforming to defined APIs to be installed on a given path. The parse server, for example repository contains an example implementation of the abolition of this pattern.
Use the following git
command to retrieve the repository:
git clone https://github.com/ParsePlatform/parse-server-example.git
Enter the parse-server-example
directory you just cloned:
cd ~/parse-server-example
Use npm
to install dependencies, including parse-server
in the current directory:
npm install
npm
will fetch all required modules parse-server
and store them in ~/parse-server-example/node_modules
.
Use npm
to start the service. This will run the command defined in the start
property of package.json
. In this case, it runs node index.js
:
npm start
> [email protected] start /home/sammy/parse-server-example
> node index.js
DATABASE_URI not specified, falling back to localhost.
parse-server-example running on port 1337.
You can terminate the running application at any time by pressing Ctrl-C.
The defined Express application index.js
passes the HTTP request to the parse-server
module, and the module communicates with the MongoDB instance and calls the function ~/parse-server-example/cloud/main.js
defined therein.
In this case, the endpoint called by Parse Server API defaults to:
http://your_server_IP/parse
In another terminal, you can use it curl
to test this endpoint. Make sure to log in to the server first, because these commands refer to localhost
instead of a specific IP address.
Create a record by sending a POST
request with the X-Parse-Application-Id
header to identify the application and some data formatted as JSON:
curl -X POST \
- H "X-Parse-Application-Id: myAppId" \
- H "Content-Type: application/json" \
- d '{"score":1337,"playerName":"Sammy","cheatMode":false}' \
http://localhost:1337/parse/classes/GameScore
{" objectId":"fu7t4oWLuW","createdAt":"2016-02-02T18:43:00.659Z"}
The data you send is stored in MongoDB and can be retrieved by sending a GET
request with curl
:
curl -H "X-Parse-Application-Id: myAppId" http://localhost:1337/parse/classes/GameScore
{" results":[{"objectId":"GWuEydYCcd","score":1337,"playerName":"Sammy","cheatMode":false,"updatedAt":"2016-02-02T04:04:29.497Z","createdAt":"2016-02-02T04:04:29.497Z"}]}
Run the function ~/parse-server-example/cloud/main.js
defined below:
curl -X POST \
- H "X-Parse-Application-Id: myAppId" \
- H "Content-Type: application/json" \
- d '{}' \
http://localhost:1337/parse/functions/hello
{" result":"Hi"}
In the original terminal, press Ctrl-C to stop the running version of the Parse Server application.
As mentioned above, six environment variables can be used to configure the example script:
Variable | Description |
---|---|
DATABASE_URI | MongoDB connection URI, such as mongodb://localhost:27017/dev |
CLOUD_CODE_MAIN | The path of the file containing Parse Cloud Code functions, such as cloud/main.js |
APP_ID | The string identifier of your application, for example myAppId |
MASTER_KEY | A secret master key that allows you to bypass the security mechanisms of all applications |
PARSE_MOUNT | Should provide the path of Parse Server API, for example /parse |
PORT | The port the application should listen on, for example 1337 |
You can set any of these values before running the script with the export
command. E.g:
export APP_ID=fooApp
It is worth reading index.js
, but in order to get a clearer understanding of what is happening, you can also write your own shorter version of the example. Open a new script in the editor:
nano my_app.js
And paste the following, changing the highlighted value as needed:
var express =require('express');var ParseServer =require('parse-server').ParseServer;
// Configure the Parse APIvar api =newParseServer({
databaseURI:'mongodb://localhost:27017/dev',
cloud: __dirname +'/cloud/main.js',
appId:'myOtherAppId',
masterKey:'myMasterKey'});
var app =express();
// Serve the Parse API on the /parse URL prefix
app.use('/myparseapp', api);
// Listen for connections on port 1337var port =9999;
app.listen(port,function(){
console.log('parse-server-example running on port '+ port +'.');});
Exit and save the file, then run it with Node.js:
node my_app.js
parse-server-example running on port 9999.
Similarly, you can press Ctrl-C to stop my_app.js
at any time. As mentioned above, the behavior of my_app.js
in this example is almost the same as the provided index.js
, except that it will listen on port 9999 and Parse Server /myparseapp
is installed, so the endpoint URL looks like this:
http: // your server IP: 9999 / myparseapp
It can be tested with curl
like this:
curl -H "X-Parse-Application-Id: myOtherAppId" http://localhost:9999/myparseapp/classes/GameScore`
You should now understand the basics of running Node.js applications (such as Parse Server) in an Ubuntu environment. Fully migrating applications from Parse can be a more complex task, requiring code changes and careful planning of the infrastructure.
For more Ubuntu tutorials, please go to [Tencent Cloud + Community] (https://cloud.tencent.com/developer?from=10680) to learn more.
Reference: "How To Run Parse Server on Ubuntu 14.04"
Recommended Posts