Skip to content

Instantly share code, notes, and snippets.

@bvlion
Created June 29, 2017 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bvlion/ff8a2be260c2b367716f87cfd1e872d2 to your computer and use it in GitHub Desktop.
Save bvlion/ff8a2be260c2b367716f87cfd1e872d2 to your computer and use it in GitHub Desktop.
Node.jsでhtmlを表示するサンプル
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
var server = http.createServer();
server.on('request', doRequest);
var fs = require('fs');
function doRequest(req, res) {
var url = req.url;
if (url.match(/html/)) { // favicon対策
fs.readFile('.' + url, 'utf-8', doReard);
}
function doReard(err, data) {
res.writeHead(200, {'Content-Type' : 'text/html'});
res.write(data);
res.end();
}
}
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment