Created
November 12, 2017 11:43
-
-
Save droganaida/4ce862b95f4f2ac63ef410cabe208b83 to your computer and use it in GitHub Desktop.
Simple API Server for adapter https://gist.github.com/droganaida/bd5ff738f75e5e77017ae6feb2f1c201
This file contains 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
const http = require('http'); | |
const url = require('url'); | |
const port = 6006; | |
const requestHandler = (request, response) => { | |
const query = url.parse(request.url, true).query; | |
if (query && query.count && (query.count > 0)) { | |
let apiObjects = [ | |
{title: "Level 1", buttonText: "I Can Win"}, | |
{title: "Level 2", buttonText: "Bring It On"}, | |
{title: "Level 3", buttonText: "Hurt Me Plenty"}, | |
{title: "Level 4", buttonText: "Hardcore"}, | |
{title: "Level 5", buttonText: "Nightmare!"} | |
]; | |
let limit = query.count; | |
if (limit > apiObjects.length) { | |
limit = apiObjects.length; | |
} | |
apiObjects = apiObjects.slice(0, limit); | |
response.end(JSON.stringify(apiObjects)); | |
} else { | |
response.writeHead(404); | |
response.end("Not Found"); | |
} | |
}; | |
const server = http.createServer(requestHandler); | |
server.listen(port, (err) => { | |
if (err) { | |
return console.log(`Internal Server Error: ${err}`); | |
} | |
console.log(`Listening on port: ${port}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment