njsetn:serving_files
var http = require("http"); var fs = require("fs"); var pas = require("path"); http.createServer(function(req, res){ console.log(`${req.method} request for ${req.url}`); if (req.url === "/") { fs.readFile("./public/index.html", "UTF-8", function(error, html) { res.write(200, {"Content-Type": "text/html"}); res.end(html); }); } else if (req.url.match(/.css$/)) { var cssPath = path.join(__dirname, 'public', req.url); var fileStream = fs.createReadStream(cssPath, "UTF-8"); res.writeHead(200, {"content-type": "text/css"}); fileStream.pipe(res); } else if (req.url.match(/.css$/)) { var imgPath = path.join(__dirname, 'public', req.url); var imgStream = fs.createReadStream(imgPath); res.writeHead(200, {"Content-Type": "image/jpeg"}); imgStream.pipe(res); } else { res.writeHead(404, {content-Type": "text/plain"}); res.end("404 File Not Found"); } }).listen(3000);
njsetn/serving_files.txt · Last modified: 2016/03/25 20:33 by leo