Skip to content

Instantly share code, notes, and snippets.

@juanvallejo
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save juanvallejo/ccefff02c0118e1eb2f3 to your computer and use it in GitHub Desktop.

Select an option

Save juanvallejo/ccefff02c0118e1eb2f3 to your computer and use it in GitHub Desktop.
Simple Node.js Server
// define runtime constants
var PORT = 8000;
// import modules needed to make the server (http and filesystem modules for now)
var http = require('http');
var fs = require('fs');
var server = http.createServer(requestHandler);
function requestHandler(request, response) {
// write the headers of our response
response.writeHead({
'Content-Type' : 'text/plain'
});
// send our response to client and end the connection
response.end("Hello World!");
}
// start the server and bind it to port 8000
server.listen(PORT);
// to run this application, go to your command line, cd into this folder, and type `node server`
// that's it, to access it through your browser, go to http://localhost:8000
@KennySutherland
Copy link

Hey... I had to type 'node simple-server' instead 'node server'... and why doesn't this work in safari? Got it working in chrome though... Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment