8
0

echo-server.js 944 B

1234567891011121314151617181920212223242526272829303132333435
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* eslint-env node */
  6. /**
  7. * Simple HTTP server which parses POST requests and returns it in JSON format.
  8. */
  9. const http = require( 'http' );
  10. const queryString = require( 'querystring' );
  11. const port = 3030;
  12. const server = http.createServer( ( req, res ) => {
  13. const methodName = req.method;
  14. let content = '';
  15. console.info( `Incoming ${ methodName } request.` );
  16. if ( methodName == 'POST' ) {
  17. res.writeHead( 200, { 'Content-Type': 'application/json' } );
  18. req.on( 'data', data => ( content += data ) );
  19. req.on( 'end', () => res.end( JSON.stringify( queryString.parse( content ) ) ) );
  20. return;
  21. }
  22. res.writeHead( 200, { 'Content-Type': 'text' } );
  23. res.end( 'Please make a POST request to get an echo response.' );
  24. } );
  25. console.info( `Starting server on port ${ port }.` );
  26. server.listen( port );