Created
July 11, 2014 06:41
-
-
Save jtyjty99999/85efb1143f5dc21c61ec to your computer and use it in GitHub Desktop.
ch1
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
| var http = require('http'), | |
| url = require('url'), | |
| path = require('path'), | |
| fs = require('fs'), | |
| config = require('./config.js') | |
| var PORT = config.PORT; | |
| var server = http.createServer(function (request, response) { | |
| response.setHeader("Server", "ICBUNBSERVER"); | |
| var pathname = url.parse(request.url).pathname; | |
| if (pathname.slice(-1) === "/") { | |
| pathname = pathname + config.Welcome.file; | |
| } | |
| var realPath = path.join("static", path.normalize(pathname.replace(/\.\./g, ""))); | |
| fs.exists(realPath, function (exists) { | |
| if (!exists) { | |
| response.writeHead(404, { | |
| 'Content-Type' : 'text/plain' | |
| }); | |
| response.write("This request URL " + pathname + " was not found on this server."); | |
| response.end(); | |
| } else { | |
| fs.readFile(realPath, "binary", function (err, file) { | |
| if (err) { | |
| response.writeHead(500, { | |
| 'Content-Type' : 'text/plain' | |
| }); | |
| response.end(err); | |
| } else { | |
| response.writeHead(200, { | |
| 'Content-Type' : 'text/html' | |
| }); | |
| response.write(file, "binary"); | |
| response.end(); | |
| } | |
| }); | |
| } | |
| }); | |
| }); | |
| server.listen(PORT); | |
| console.log("Server runing at port: " + PORT + "."); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment