Created
June 29, 2017 02:35
-
-
Save bvlion/ff8a2be260c2b367716f87cfd1e872d2 to your computer and use it in GitHub Desktop.
Node.jsでhtmlを表示するサンプル
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
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