Skip to content

Instantly share code, notes, and snippets.

@aclave1
Last active August 29, 2015 14:04
Show Gist options
  • Save aclave1/8c27f51a862094451772 to your computer and use it in GitHub Desktop.
Save aclave1/8c27f51a862094451772 to your computer and use it in GitHub Desktop.
Simplest possible node.js web server
var http = require('http');
var fileReader = require('fs');
http.createServer(function(req,res){
var path = __dirname+req.url;
fileReader.readFile(path,function(err,fl){
res.end(fl);
});
}).listen(8080);
<html>
<head></head>
<body>
<p>Hello world!</p>
</body>
</html>
@aclave1
Copy link
Author

aclave1 commented Jul 16, 2014

To setup:

  • Install node.js http://nodejs.org/download/
  • make a folder, we'll call it mywebsite
  • Move this file to your mywebsite folder, name it app.js
  • To make sure everything works, open up your terminal and type: node mywebsite/app.js
  • To stop the server type ctrl+c or cmd+c on a mac
  • After that, place your html files into your mywebsite folder, lets say you have one named index.html
  • start the node server again with: node mywebsite/app.js
  • Open your web browser and type: localhost:8080
  • You should see your html file in your browser!

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