Last active
August 29, 2015 14:14
-
-
Save juanvallejo/ccefff02c0118e1eb2f3 to your computer and use it in GitHub Desktop.
Simple Node.js Server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!