-
-
Save onegrx/26a4381f84f8e78ce0da to your computer and use it in GitHub Desktop.
Before running, install express by running npm install express and run with: node absurd_rest_service.js in this script's directory.
Then: To submit calculation to curl -i -H "Content-Type: application/json" -d '{ "operation" : "+", "operands" : [ 2,3] }' http://localhost:3000/calculations To see results, use link from response: curl http://loca…
This file contains hidden or 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
/* | |
Before running, install express by running | |
npm install express | |
and run with: | |
node absurd_rest_service.js | |
in this script's directory. | |
Then: | |
To submit calculation to | |
curl -i -H "Content-Type: application/json" -d '{ "operation" : "+", "operands" : [ 2,3] }' http://localhost:3000/calculations | |
curl http://localhost:3000/results/1 | |
*/ | |
var express = require('express'); | |
var events = require('events'); | |
var app = express(); | |
app.use(express.bodyParser()); | |
var sequence = 0; // poor man's sequence | |
var results = []; | |
function execute(operation) { | |
if (operation.operation === '+') { | |
return operation.operands[0] + operation.operands[1]; | |
} else if (operation.operation === '*') { | |
return operation.operands[0] * operation.operands[1]; | |
} | |
} | |
app.post('/calculations', function (req, res) { | |
res.setHeader('Content-Type', 'application/json'); | |
sequence++; | |
results[sequence] = execute(req.body); | |
res.status(202); | |
res.json({ outcome: "success", links: { result: "http://localhost:3000/results/" + sequence } }); | |
}); | |
app.get('/results/:id', function (req, res) { | |
res.setHeader('Content-Type', 'application/json'); | |
res.json({ result: results[req.params['id']] }); | |
}); | |
app.listen(3000); | |
console.log("App listening on port 3000"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment