forked from parse-community/parse-server-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
154 lines (124 loc) · 5.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Example express application adding the parse-server module to expose Parse
// compatible API routes.
var express = require('express');
var ParseServer = require('parse-server').ParseServer;
var path = require('path');
var jquery = require("jquery");
var Pusher = require('pusher');
var databaseUri = process.env.DATABASE_URI || 'mongodb://heroku_rtzfhkmq:go3abk4dfibc9gniqndvoclhdd@ds133328.mlab.com:33328/heroku_rtzfhkmq';
var api = new ParseServer({
databaseUri: process.env.MONGODB_URI || 'mongodb://heroku_rtzfhkmq:go3abk4dfibc9gniqndvoclhdd@ds133328.mlab.com:33328/heroku_rtzfhkmq',
cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
appId: process.env.APP_ID || 'Sup3rS0da',
masterKey: process.env.MASTER_KEY || 'frfnieurnfddc4rf34fe', //Add your master key here. Keep it secret!
serverURL: process.env.SERVER_URL || 'http://supernatural-sodas-parse.herokuapp.com/parse', // Don't forget to change to https if needed
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
var pusher = new Pusher({
appId: '310535',
key: '1e3d02f7d556d73e3888',
secret: '4afe659880e146c9ff2a',
encrypted: true
});
if (!databaseUri) {
console.log('DATABASE_URI not specified, falling back to localhost.');
}
var app = express();
var bodyParser = require('body-parser')
app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
// 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);
// There will be a test page available on the /test path of your server url
// Remove this before launching your app
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, '/public/site.html'));
});
// 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!');
});
var port = process.env.PORT || 1337;
var httpServer = require('http').createServer(app);
httpServer.listen(port, function() {
console.log('parse-server-example running on port ' + port + '.');
});
// This will enable the Live Query real-time server
ParseServer.createLiveQueryServer(httpServer);
// Creat class Supernatural Sodas
var SupernaturalSodas = Parse.Object.extend("SupernaturalSodas");
// Process equest to create a new supernaturalSoda
app.post('/save', function(req, res) {
// create new instance of Class
var supernaturalSoda = new SupernaturalSodas();
// write attributes to instance of class
for (object in req.body) {
supernaturalSoda.set(object, req.body[object]);
}
// save class instance
supernaturalSoda.save(null, {
success: function(supernaturalSoda) {
// Execute any logic that should take place after the object is saved
res.sendStatus(200);
pusher.trigger('sodas', 'test_event', jsonArray);
},
error: function(supernaturalSoda, error) {
// Execute any logic that should take place if the save fails.
// error is a Parse.Error with an error code and message.
console.log('Failed to create new object, with error code: ' + error.message);
}
});
});
// Get a list of names and quantities of all the SupernaturalSodas
app.get('/inventory', function(req, res) {
// creates new search
var query = new Parse.Query(SupernaturalSodas);
// limits the results by the criteria
// query.greaterThan("sodaQuantity", 0);
query.find({
success: function(feeds) {
var jsonArray = [];
// console.log("Successfully retrieved " + results.length + " Sodas");
// Do something with the returned Parse.Object values
for (var i = 0; i < feeds.length; i++) {
jsonArray.push(feeds[i].toJSON());
console.log(jsonArray);
}
// res.sendStatus(200);
res.json(jsonArray);
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
});
// Get a list of names and quantities of all the SupernaturalSodas
// app.get('/inventory', function(req, res) {
// // creates new search
// var query = new Parse.Query(SupernaturalSodas);
// // limits the results by the criteria
// // query.greaterThan("sodaQuantity", 0);
// query.find({
// success: function(supernaturalsodas) {
// supernaturalsodas = supernaturalsodas.map(function(supernaturalsoda) {
// return supernaturalsoda.toJSON();
// }),
// console.log('supernaturalsodas post .toJSON()', supernaturalsodas);
// // Return the JSON to frontend:
// response.success(supernaturalsodas);
// },
// error: function(error) {
// alert("Error: " + error.code + " " + error.message);
// }
// });
// });