server.js 705 B

12345678910111213141516171819202122232425262728293031323334
  1. import connect from 'connect';
  2. import cowsay from 'cowsay';
  3. import path from 'path';
  4. import portscanner from 'portscanner';
  5. import serveStatic from 'serve-static';
  6. // Configuration for the server.
  7. const PORT = 9999;
  8. const MAX_PORT = PORT + 100;
  9. const HOST = '127.0.0.1';
  10. const app = connect();
  11. const verbs = [
  12. 'Chewing the cud',
  13. 'Grazing',
  14. 'Mooing',
  15. 'Lowing',
  16. 'Churning the cream'
  17. ];
  18. app.use(serveStatic(path.join(__dirname, '..')));
  19. portscanner.findAPortNotInUse(PORT, MAX_PORT, HOST, (error, port) => {
  20. if (error) {
  21. throw error;
  22. }
  23. process.stdout.write(cowsay.say({
  24. text: `${verbs[Math.floor(Math.random() * 5)]} on ${HOST}:${port}`
  25. }) + '\n\n');
  26. app.listen(port);
  27. });