Skip to content

Instantly share code, notes, and snippets.

@JonnySchnittger
Created July 17, 2019 23:28
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 JonnySchnittger/c6eed8eed03d4366ac6acb100ec44265 to your computer and use it in GitHub Desktop.
Save JonnySchnittger/c6eed8eed03d4366ac6acb100ec44265 to your computer and use it in GitHub Desktop.
Embed a http server for persistence within Slack
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