Wednesday, 30 March 2016

How to start your program as a web application ?


Create a JS file (test.js)
1. Import "http" module
2. Create HTTP server with a HTTP port
3. Write response code, response headers and body contents
4. Write logs on console (if you need for debug)

Example
// Import http module
const http = require('http');

// Create HTTP server which listens on port 8124http.createServer( (request, response) => {
  // Write the response code = 200 and header "content type"

  response.writeHead(200, {'Content-Type': 'text/plain'});

  // Write the response body
  response.end('Hello World');
}).listen(8124);


// Write log to console
console.log('Access the server using http://localhost:8004/');



Execute the JS to start the HTTP server
$ node test.js

Test the server
http://localhost:8004/
http://127.0.0.1:8004/

No comments:

Post a Comment

Note: only a member of this blog may post a comment.