Skip to content

Instantly share code, notes, and snippets.

@ronfe
Created May 31, 2016 15:11
Show Gist options
  • Save ronfe/15e02390bea347898fb199947667d362 to your computer and use it in GitHub Desktop.
Save ronfe/15e02390bea347898fb199947667d362 to your computer and use it in GitHub Desktop.
var http = require("http");
var url = require("url");
var fs = require("fs");
var server = http.createServer(function (request, response) {
var ua = request.headers['user-agent'];
console.log(ua);
var urlObj = url.parse(request.url, true);
var pathname = urlObj.pathname;
console.log(pathname)
var reg = /\.html/i;
if (reg.test(pathname)) {
console.log("passed " + pathname);
var con = fs.readFileSync("." + pathname, "utf8");
response.writeHead(200, {"content-type": "text/html;charset=utf-8"});
response.end(con);
}
//img/bg2.jpg
var reg1 = /\.jpg/i;
if (reg1.test(pathname)) {
var img = fs.readFileSync("." + pathname);
response.writeHead(200, {"content-type": "image/jpg;charset=utf-8;"});
response.end(img);
}
});
server.listen(8801, function () {
console.log("port 8801");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment