這篇文章是從零開始使用ParseServe的更新版. ParseServe是一個簡單的, 開箱即用的開源后端服務(wù). 可以用來快速搭建一個配置服務(wù)或者是簡單的存儲服務(wù).
使用
ParseServe可以和NodeJs的express服務(wù)來啟動. 這也是一個比較正確的方式用來在一臺機(jī)器上快速使用ParseServe. 具體做法可以參考其github頁面上express相關(guān)的部分
其中, 一個關(guān)鍵的做法是寫一個正確的入口文件(index.js)
// Parse Server and Parse Dashboard
var express = require('express');
var ParseServer = require('parse-server').ParseServer;
var ParseDashboard = require('parse-dashboard');
var path = require('path');
var appId = process.env.APP_ID || 'AppId';
var appName = process.env.APP_NAME || 'AppName';
var masterKey = process.env.MASTER_KEY || 'mk1'; //Add your master key here. Keep it secret!, it's required by dashboard.
var serverURL = process.env.SERVER_URL || 'http://localhost:1337/parse'; // Don't forget to change to https if needed
var databaseUri = process.env.DATABASE_URI || process.env.MONGODB_URI;
if (!databaseUri) {
console.log('DATABASE_URI not specified, falling back to localhost.');
}
var api = new ParseServer({
databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
appId: appId,
masterKey: masterKey,
serverURL: serverURL,
liveQuery: {
classNames: ["Posts", "Comments"] // List of classes to support for query subscriptions
}
});
// Client-keys like the javascript key or the .NET key are not necessary with parse-server
// If you wish you require them, you can set them as options in the initialization above:
// javascriptKey, restAPIKey, dotNetKey, clientKey
///////////////////////////////////////////////////////////////////////////////
//Dashboard
///////////////////////////////////////////////////////////////////////////////
var dashboard = new ParseDashboard({
"apps": [
{
"serverURL": serverURL,
"appId": appId,
"masterKey": masterKey,
"appName": appName
}
],
"users": [
{
"user":"milo",
"pass":"$2y$10$yryCWSnQgB893jH42.I1oei1jNd3rHR2qmkMrHlYUdolOe0OzKqky"
},
{
"user":"admin",
"pass":"$2y$10$IdaoDlon7NLpfAfgVfo0/euIU4lHHoQZT7i5UxKdLDVZz1MuBSGgq"
}
],
"useEncryptedPasswords": true
});
var app = express();
// Serve static assets from the /public folder
app.use('/public', express.static(path.join(__dirname, '/public')));
// Serve the Parse API on the /parse URL prefix
var mountPath = process.env.PARSE_MOUNT || '/parse';
app.use(mountPath, api);
// Serve the Parse API on the /parse URL prefix
var dashboardPath = process.env.DASHBOARD_MOUNT || '/dashboard';
app.use(dashboardPath, dashboard);
// // Parse Server plays nicely with the rest of your web routes
// app.get('/', function (req, res) {
// res.status(200).send('I dream of being a website. Please star the parse-server repo on GitHub!');
// });
// There will be a test page available on the /test path of your server url
// Remove this before launching your app
app.get('/test', function (req, res) {
res.sendFile(path.join(__dirname, '/public/test.html'));
});
// Serve static assets from the /public folder
app.use('/', express.static(path.join(__dirname, '/web')));
// For Todo
app.get('/todo', function (req, res) {
res.sendFile(path.join(__dirname, '/web/index.html'));
});
var port = process.env.PORT || 1337;
var httpServer = require('http').createServer(app);
httpServer.listen(port, function () {
console.log('parse-server-example running on 127.0.0.1:' + port + '.');
});
// This will enable the Live Query real-time server
ParseServer.createLiveQueryServer(httpServer);
部署
使用PM2來部署nodejs應(yīng)用已經(jīng)是一個非常通用的方案了.
一個比較實用的方法是寫一個*.yml文件, 比如deploybypm2.yml
apps:
- script : index.js
name : 'parse-server'
instances: 0
exec_mode: cluster
watch : true
env :
NODE_ENV: development
env_production:
NODE_ENV: production
然后執(zhí)行
pm2 start deploybypm2.yml
就可以看到pm2根據(jù)系統(tǒng)的cpu核數(shù)自動啟動了多個進(jìn)程開始運行parse server了.