快速使用ParseServe
2018-06-01 本文已影响14人
每天多一点
这篇文章是从零开始使用ParseServe的更新版. ParseServe是一个简单的, 开箱即用的开源后端服务. 可以用来快速搭建一个配置服务或者是简单的存储服务.
使用
ParseServe可以和NodeJs的express服务来启动. 这也是一个比较正确的方式用来在一台机器上快速使用ParseServe. 具体做法可以参考其github页面上express相关的部分
其中, 一个关键的做法是写一个正确的入口文件(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应用已经是一个非常通用的方案了.
一个比较实用的方法是写一个*.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
然后执行
pm2 start deploybypm2.yml
就可以看到pm2根据系统的cpu核数自动启动了多个进程开始运行parse server了.