Last active
August 14, 2017 02:47
-
-
Save Hinaser/c639a8462d2bf97565a8a80981ee40cc to your computer and use it in GitHub Desktop.
Parse server getting started sample.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Sample parse-server/parse-dashboard script for Node.js v6. | |
* | |
* In order to use this script you must follow the procedure below. | |
* | |
* 1) Create folder for this snippet. | |
* $ cd <A folder you like to host script files> | |
* $ npm init | |
* # You don't need to answer all of the questions. You can safely skip this mess. | |
* $ npm install --save-dev babel-cli babel-preset-es2015 deepmerge express parse-dashboard parse-server simple-parse-smtp-adapter | |
* | |
* 2) Modify package.conf with the command below. | |
* $ node -p 'f="parse-server-sample.js";p=require("./package.json");p.scripts.start="babel-node "+f;p.main=f;p.babel={presets:["es2015"]};JSON.stringify(p, null, 2);' > 'package.json.new' | |
* $ mv package.json.new package.json | |
* | |
* 3) Create config.json in the same directory as this code. | |
* config.json example: | |
* | |
* "general": { | |
* "protocol": "http", | |
* "host": "localhost", | |
* "port": 28080, | |
* "apiRoot": "parse", | |
* "apiVer": "1", | |
* "dashBoardAuth": "admin:fda893kldfa.DKd93" | |
* }, | |
* "parseServer": { | |
* "appId": "an-app-id", | |
* "appName": "Demo application", | |
* "masterKey": "ffffffffffffffffffffffffffffffffffffffff", | |
* "fileKey": "ffffffff-ffff-ffff-ffff-ffffffffffff", | |
* "databaseURI": "mongodb://localhost:27017/dev", | |
* "publicServerURL": "http://localhost:28080/parse/v1", | |
* "verifyUserEmails": false, | |
* "emailAdapter": { | |
* "options": { | |
* "fromAddress": "noreply@example.com", | |
* "user": "smtp-user@example.com", | |
* "password": "smtp-user-password", | |
* "host": "smtp.example.com", | |
* "isSSL": true, | |
* "port": 465, | |
* "name": "example.com" | |
* } | |
* } | |
* } | |
* | |
* 4) Copy this snippet to a file named 'parse-server-sample.js' in the folder you created. | |
* $ vi parse-server-sample.js | |
* | |
* 5) Create cloud.js in the same folder. | |
* $ echo -n > cloud.js | |
* # You may write cloud code into this file in case you know how and what. | |
* | |
* 6) Run database | |
* $ sudo service <mongod|postgresql> start | |
* # I don't cover how to configure database here. | |
* | |
* 7) Run parse server and dashboard | |
* $ npm start | |
* | |
*/ | |
import path from 'path'; | |
import express from 'express'; | |
import {ParseServer} from 'parse-server'; | |
import ParseDashboard from 'parse-dashboard'; | |
import deepmerge from 'deepmerge'; | |
const config = require('./config.json'); | |
const defaultConfig = { | |
general: { | |
protocol: "http", | |
host: "localhost", | |
port: 28080, | |
apiRoot: "parse", | |
apiVer: "1", | |
isDevelopment: process.env.NODE_ENV !== 'production', | |
dashBoardAuth: 'admin:fda893kldfa.DKd93', | |
}, | |
parseServer: { | |
cloud: path.resolve(__dirname, 'cloud.js'), | |
// "serverURL": `auto-generated by script`, | |
appId: 'an-app-id', | |
appName: 'Demo application', | |
masterKey: 'ffffffffffffffffffffffffffffffffffffffff', | |
fileKey: 'ffffffff-ffff-ffff-ffff-ffffffffffff', | |
databaseURI: 'mongodb://localhost:27017/dev', | |
publicServerURL: "http://localhost:28080/parse/1", | |
verifyUserEmails: false, | |
emailAdapter: { | |
module: "simple-parse-smtp-adapter", | |
options: { | |
fromAddress: "noreply@example.com", | |
user: "smtp-user@example.com", | |
password: "smtp-user-password", | |
host: "smtp.example.com", | |
isSSL: true, | |
port: 465, | |
name: "example.com" | |
} | |
} | |
}, | |
dashBoard: { | |
// appId: `auto-generated`, | |
// serverURL: `auto-generated`, | |
// masterKey: `auto-generated`, | |
// appName: `auto-generated`, | |
} | |
}; | |
export class APIServer { | |
constructor(){ | |
this.CONFIG = deepmerge.all([defaultConfig, config]); | |
this.CONFIG.parseServer.serverURL = this.serverURL(); | |
this.CONFIG.dashBoard.appId = this.CONFIG.parseServer.appId; | |
this.CONFIG.dashBoard.appName = this.CONFIG.parseServer.appName; | |
this.CONFIG.dashBoard.serverURL = this.CONFIG.parseServer.publicServerURL; | |
this.CONFIG.dashBoard.masterKey = this.CONFIG.parseServer.masterKey; | |
this.isMounted = false; | |
this.server = null; | |
this.app = express(); | |
} | |
serverURL(conf=null){ | |
const cg = conf ? conf.general : this.CONFIG.general; | |
return `${cg.protocol}://${cg.host}:${cg.port}` + this.apiPath(); | |
} | |
apiPath(conf=null){ | |
const cg = conf ? conf.general : this.CONFIG.general; | |
let path = ""; | |
path += cg.apiRoot ? `/${cg.apiRoot}` : ''; | |
path += cg.apiVer ? `/${cg.apiVer}` : ''; | |
return path; | |
} | |
mount(){ | |
this.app.use( | |
this.apiPath(), | |
new ParseServer(this.CONFIG.parseServer) | |
); | |
if (this.CONFIG.general.isDevelopment) { | |
let users; | |
if (this.CONFIG.general.dashBoardAuth) { | |
let [user, pass] = this.CONFIG.general.dashBoardAuth.split(':'); | |
users = [{user, pass}]; | |
} | |
this.app.use( | |
'/dashboard', | |
ParseDashboard({ | |
apps: [this.CONFIG.dashBoard], | |
users, | |
}, this.CONFIG.general.isDevelopment), | |
); | |
} | |
this.app.use('/', (req, res) => res.redirect('/dashboard')); | |
this.isMounted = true; | |
} | |
start(){ | |
if(!this.isMounted){ | |
this.mount(); | |
} | |
if(this.CONFIG.general.isDevelopment){ | |
console.log(this.CONFIG); | |
} | |
this.server = this.app.listen(this.CONFIG.general.port, () => console.log( | |
`Server is now running in ${process.env.NODE_ENV || 'development'} mode on ${this.CONFIG.general.protocol}://${this.CONFIG.general.host}:${this.CONFIG.general.port}` | |
)); | |
} | |
} | |
if (require.main === module) { | |
const apiServer = new APIServer(); | |
apiServer.start(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment