Skip to content

Instantly share code, notes, and snippets.

@jinlong
Created January 15, 2014 07:59
Show Gist options
  • Save jinlong/8432450 to your computer and use it in GitHub Desktop.
Save jinlong/8432450 to your computer and use it in GitHub Desktop.
Node.js as a simple static files server
var fs = require("fs");
var express = require("express");
var host = "127.0.0.1";
var port = 1234;
var index = fs.readFileSync('index.html');
var app = express();
app.use(app.router); //use both root and other routes below
app.use(express.static(__dirname + "/")); //use static files in ROOT/public folder
app.get("/", function(request, response){ //root dir
if(index){
response.set('Content-Type', 'text/html');
response.send(index);
}else{
response.send("Hello! It works!");
}
});
app.listen(port, host);
console.log("Static file server running at => http://localhost:" + port + "/ CTRL + C to shutdown");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment