Skip to content

Instantly share code, notes, and snippets.

@stanographer
Last active July 4, 2017 19:47
Show Gist options
  • Save stanographer/b599cc94946a2b81ac6c5f70fa6a8798 to your computer and use it in GitHub Desktop.
Save stanographer/b599cc94946a2b81ac6c5f70fa6a8798 to your computer and use it in GitHub Desktop.
'use strict';
// Use Express as our routing middleware.
const express = require('express');
const app = express();
const port = process.env.port || 4000;
startServer();
function startServer () {
// Stores all requests to /set in this arr.
var items = [];
app.get('/', function (req, res) {
res.send('Hey! I\'m awake!');
});
app.get('/set', function (req, res) {
// When a request is received on set, key-val pair object is set to kvp.
var kvp = req.query;
items.push(kvp);
res.send(items);
});
app.get('/get', function (req, res) {
// Prints the returned result to the page.
res.send('Key supplied: ' + req.query.key + ', ' +
'Found value: ' + findMe(req.query.key));
// Iterates through the array looking for the key. Returns found value.
function findMe (key) {
for (var i = 0; i <= items.length; i++) {
for (var k in items[i]) {
if (items[i].hasOwnProperty(k) && k === key) {
return items[i][k];
}
}
}
}
});
app.listen(port, () => {
console.log('I\'m up and running on port ' + port + '!');
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment