Advertisement
NLinker

Simplest server ever

Jul 3rd, 2017
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ### Installation
  2.  
  3. Install the dependencies
  4.  
  5. ```
  6. npm install express --save
  7. npm install body-parser --save
  8. npm install cookie-parser --save
  9. npm install multer --save
  10. ```
  11.  
  12. and now you can run
  13. ```
  14. node server.js
  15. ```
  16. -----------------------------------------------------------------
  17.  
  18. var express = require('express');
  19. var app = express();
  20.  
  21. app.get('/', function(req, res) {
  22.     var t = randomInt(1000, 2000);
  23.     sleep(t, function () {
  24.         res.send('' + t);
  25.     });
  26. });
  27.  
  28. function randomInt(min, max) {
  29.   return Math.floor(Math.random() * (max - min)) + min;
  30. }
  31.  
  32. function sleep(time, callback) {
  33.     var stop = new Date().getTime();
  34.     while(new Date().getTime() < stop + time) {
  35.     }
  36.     callback();
  37. }
  38.  
  39. var server = app.listen(8081, function () {
  40.    var host = server.address().address;
  41.    var port = server.address().port;
  42.    
  43.    console.log("Example app listening at http://%s:%s", host, port);
  44. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement