Skip to content

Instantly share code, notes, and snippets.

@MichMich
Created April 13, 2015 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichMich/e8a7128e361a76609b7b to your computer and use it in GitHub Desktop.
Save MichMich/e8a7128e361a76609b7b to your computer and use it in GitHub Desktop.
// These two lines are required to initialize Express in Cloud Code.
express = require('express');
app = express();
// Set the required API key.
var apiKey = 'supersecretapikey';
// Global app configuration section
app.use(express.bodyParser()); // Middleware for reading request body
// This is an example of hooking up a request handler with a specific request
// path and HTTP verb using the Express routing API.
app.post('/sensor', function(req, res) {
// Get the request variables.
var requestApiKey = req.headers['x-parse-rest-api-key'];
var identifier = req.body.identifier;
var state = req.body.state;
// Check the API key.
if (requestApiKey === undefined || requestApiKey !== apiKey) {
res.status(401).send({error:"Missing or incorrect API-key."});
return;
}
// Validate the identifier.
if (identifier === undefined || identifier.length <= 0 || typeof identifier !== "string") {
res.status(400).send({error:"Identifier missing or not a valid string."});
return;
}
// Validate the state.
if (state === undefined || typeof state !== "boolean") {
res.status(400).send({error:"State missing or not a boolean."});
return;
}
// Convert the identifier to lowercase.
identifier = identifier.toLowerCase();
// Create the Sensor and Log object.
var Sensor = Parse.Object.extend("Sensor");
var Log = Parse.Object.extend("Log");
// Look up the sensor object in the Sensor table.
var query = new Parse.Query(Sensor);
query.equalTo("identifier", identifier);
query.first({
success: function(sensor) {
// If the Sensor for the requested identifier is not found. Create a new object.
if (!sensor) {
var sensor = new Sensor();
sensor.set('identifier', identifier);
}
// Set the requested state for the sensor.
sensor.set('state', state);
// Save the (new) sensor with updated state.
sensor.save(null, {
success: function (sensor) {
// If saving finished, create a new log object with a pointer to the sensor.
var log = new Log();
log.set('sensor', sensor);
log.set('identifier', identifier);
log.set('state', state);
log.save(null, {
success: function (log) {
// Everything ok. Report back to the client.
res.send({success:true, identifier:identifier, state:state});
},
error: function (error) {
// Error saving the log. Report error to client.
res.status(400).send({error:"Log save error: " + error.code + " " + error.message});
}
});
},
error: function (error) {
// Error saving the sensor object. Report error to client.
res.status(400).send({error:"Sensor save error: " + error.code + " " + error.message});
}
});
},
error: function(error) {
// Error performing the initial query. Report error to client.
res.status(400).send({error:"Query error: " + error.code + " " + error.message});;
}
});
});
// Enable the express webserver.
app.listen();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment