server.js 997 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /**
  2. * Created by Tony on 11/1/13.
  3. */
  4. var http = require('http');
  5. var sockjs = require('sockjs');
  6. var Hapi = require('hapi');
  7. // 1. Echo sockjs server
  8. var sockjs_opts = {
  9. sockjs_url: "http://cdn.jsdelivr.net/sockjs/1.0.1/sockjs.min.js"
  10. };
  11. var sockjs_echo = sockjs.createServer(sockjs_opts);
  12. sockjs_echo.on('connection', function(conn) {
  13. conn.on('data', function(message) {
  14. conn.write(message);
  15. });
  16. });
  17. // Create a server and set port (default host 0.0.0.0)
  18. var hapi_server = new Hapi.Server();
  19. hapi_server.connection({
  20. port: 9999
  21. });
  22. hapi_server.register(require('inert'), (err) => {
  23. hapi_server.route({
  24. method: 'GET',
  25. path: '/{path*}',
  26. handler: function(request, reply) {
  27. reply.file('./html/index.html');
  28. }
  29. });
  30. });
  31. //hapi_server.listener is the http listener hapi uses
  32. sockjs_echo.installHandlers(hapi_server.listener, {
  33. prefix: '/echo'
  34. });
  35. console.log(' [*] Listening on 0.0.0.0:9999');
  36. hapi_server.start();