Skip to content

Instantly share code, notes, and snippets.

@fgo
Last active April 20, 2017 11:17
Show Gist options
  • Select an option

  • Save fgo/d42f4f1cad786f5ff2e09e49ca943ec8 to your computer and use it in GitHub Desktop.

Select an option

Save fgo/d42f4f1cad786f5ff2e09e49ca943ec8 to your computer and use it in GitHub Desktop.
Hello HTTP - A simple web server example in Node.js
// Hello HTTP
// A simple web server example in Node.js
// Include HTTP module to create the server.
const http = require('http')
// Create data payload
let payload = JSON.stringify({message: 'Hello, World!'});
// Create HTTP server to display message "Hello, World!" in JSON.
const server = http.createServer(function(req, res) {
res.writeHead(200, {"Content-Type": "application/json"});
res.end(payload);
});
// Set server to listen on port 3000 (http://localhost:3000)
// Visit http://localhost:300 on the browser or curl:
// curl -v http://localhost:3000
// * Rebuilt URL to: http://localhost:3000/
// * Trying ::1...
// * Connected to localhost (::1) port 3000 (#0)
// > GET / HTTP/1.1
// > Host: localhost:3000
// > User-Agent: curl/7.43.0
// > Accept: *[>
// >
// < HTTP/1.1 200 OK
// < Content-Type: application/json
// < Date: Thu, 20 Apr 2017 10:40:05 GMT
// < Connection: keep-alive
// < Transfer-Encoding: chunked
// <
// * Connection #0 to host localhost left intact
// {"message":"Hello, World!"}%
server.listen(3000);
// Tell terminal that server is running
console.log("Server is listening on http://localhost:3000")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment