Store and publish annotations on the web, as described in the Web Annotation Discovery proposal.

web-annotation-discovery-se.../bin/ www.ts
90 lines
1.5 KiB

  1. #!/usr/bin/env node
  2. /**
  3. * Module dependencies.
  4. */
  5. import app from '../app.js'
  6. import http from 'http'
  7. /**
  8. * Get port from environment and store in Express.
  9. */
  10. var port = normalizePort(process.env.PORT || '8080');
  11. app.set('port', port);
  12. /**
  13. * Create HTTP server.
  14. */
  15. var server = http.createServer(app);
  16. /**
  17. * Listen on provided port, on all network interfaces.
  18. */
  19. server.listen(port);
  20. server.on('error', onError);
  21. server.on('listening', onListening);
  22. /**
  23. * Normalize a port into a number, string, or false.
  24. */
  25. function normalizePort(val: string) {
  26. var port = parseInt(val, 10);
  27. if (isNaN(port)) {
  28. // named pipe
  29. return val;
  30. }
  31. if (port >= 0) {
  32. // port number
  33. return port;
  34. }
  35. return false;
  36. }
  37. /**
  38. * Event listener for HTTP server "error" event.
  39. */
  40. function onError(error: any) {
  41. if (error.syscall !== 'listen') {
  42. throw error;
  43. }
  44. var bind = typeof port === 'string'
  45. ? 'Pipe ' + port
  46. : 'Port ' + port;
  47. // handle specific listen errors with friendly messages
  48. switch (error.code) {
  49. case 'EACCES':
  50. console.error(bind + ' requires elevated privileges');
  51. process.exit(1);
  52. break;
  53. case 'EADDRINUSE':
  54. console.error(bind + ' is already in use');
  55. process.exit(1);
  56. break;
  57. default:
  58. throw error;
  59. }
  60. }
  61. /**
  62. * Event listener for HTTP server "listening" event.
  63. */
  64. function onListening() {
  65. var addr = server.address();
  66. var bind = typeof addr === 'string'
  67. ? 'pipe ' + addr
  68. : 'port ' + addr?.port;
  69. console.log('Listening on %s', bind)
  70. }