Created
July 17, 2019 23:28
-
-
Save JonnySchnittger/c6eed8eed03d4366ac6acb100ec44265 to your computer and use it in GitHub Desktop.
Embed a http server for persistence within Slack
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
document.addEventListener("DOMContentLoaded", function () { | |
const http = require('http'); | |
const url = require('url'); | |
const { spawnSync } = require('child_process'); | |
const port = 7000; | |
const contentType = { "Content-Type": "text/plain" }; | |
const httpVerb = { | |
GET: "GET", | |
POST: "POST" | |
}; | |
const httpCode = { | |
Success: 200, | |
NotFound: 404 | |
}; | |
const handlers = { | |
GET: { | |
'/': function (request, response, uri) { | |
response.writeHead(httpCode.Success, contentType); | |
}, | |
'/do-something': function (request, response, uri) { | |
} | |
}, | |
POST: { | |
'/': function (request, response, uri) { | |
var body = ""; | |
request.on("data", function (chunk) { | |
body += chunk; | |
}); | |
request.on("end", function () { | |
response.writeHead(httpCode.Success, contentType); | |
response.end(body); | |
}); | |
} | |
} | |
}; | |
const server = http.createServer((function (request, response) { | |
var requestUri = url.parse(request.url, true); | |
if ((!handlers.hasOwnProperty(request.method)) || (!handlers[request.method].hasOwnProperty(requestUri.pathname))) { | |
response.writeHead(httpCode.NotFound, contentType); | |
} else { | |
handlers[request.method][requestUri.pathname](request, response, requestUri); | |
} | |
response.end(); | |
})); | |
server.listen(port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment