index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. const http = require('http');
  2. const log = require('hlogger').createLogger('hci-http');
  3. var activator = {
  4. start(context) {
  5. this.server = http.createServer((req,res) => {
  6. var lurl = req.url.replace(/^\/|\/$/g,''); // Replace all starting "/" or ending "/"
  7. // New
  8. context
  9. .channel(context.name).channel(req.method)
  10. .propagate(lurl,req,res)
  11. .done((e) => {
  12. if(e.count == 0) {
  13. res.writeHead(404,"Not found");
  14. res.end("Not found");
  15. console.log("Every thing is done");
  16. }
  17. });
  18. // Different way
  19. });
  20. //
  21. log.info("Server started at: " + 3500);
  22. this.server.listen(3500);
  23. this.clients = {};
  24. this.server.on('connection',(conn) => {
  25. var key = conn.remoteAddress + ':' + conn.remotePort;
  26. this.clients[key] = conn;
  27. conn.on('close',() => {
  28. delete this.clients[key];
  29. });
  30. });
  31. this.server.on('close',() => {
  32. log.info("Http server went down");
  33. for(var k in this.clients) {
  34. this.clients[k].destroy();
  35. };
  36. });
  37. },
  38. stop(context) {
  39. log.info("Server shutting down");
  40. this.server.close() // Shutdown clients
  41. for(var k in this.clients) {
  42. this.clients[k].destroy();
  43. };
  44. //this.server.emit('user:close');
  45. }
  46. }
  47. module.exports.bundleActivator = activator;