8
0

index.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* eslint-env node */
  6. const http = require( 'http' );
  7. const hostname = '127.0.0.1';
  8. const port = 3000;
  9. const server = http.createServer( function( req, res ) {
  10. res.statusCode = 200;
  11. res.setHeader( 'Content-Type', 'application/json' );
  12. readEntries( getTimeout() ).then( entries => {
  13. res.setHeader( 'Access-Control-Allow-Origin', '*' );
  14. res.setHeader( 'Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept' );
  15. res.end( JSON.stringify( entries ) + '\n' );
  16. } );
  17. } );
  18. server.listen( port, hostname, () => {
  19. console.log( `server running at http://${ hostname }:${ port }/` );
  20. } );
  21. function readEntries( timeOut ) {
  22. return new Promise( resolve => {
  23. setTimeout( () => {
  24. resolve( [
  25. { id: '@jodator', name: 'Maciej Gołaszewski', userId: 123 }
  26. ] );
  27. }, timeOut );
  28. } );
  29. }
  30. function getTimeout() {
  31. const type = parseInt( Math.random() * 10 );
  32. // 80% of requests completes in 100-300ms range (10ms resolution).
  33. if ( type < 8 ) {
  34. return parseInt( 10 + Math.random() * 20 ) * 10;
  35. }
  36. // 20% of requests completes in 600-1000ms range (10ms resolution).
  37. return parseInt( 60 + Math.random() * 40 ) * 10;
  38. }