Created
January 15, 2014 07:59
-
-
Save jinlong/8432450 to your computer and use it in GitHub Desktop.
Node.js as a simple static files server
This file contains 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
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