Created
July 24, 2020 06:58
-
-
Save pandanote-info/7aed8792bda7bd93c49541aad9f2f2de to your computer and use it in GitHub Desktop.
Node.js用staticな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 fs = require('fs'); | |
const server = http.createServer | |
(function (request, response) { | |
var urlp = request.url; | |
if (別のURLの場合) { | |
// 中略 | |
} else { | |
if (urlp == "/") { | |
urlp += "index.html"; | |
} | |
urlp = "static"+urlp; | |
try { | |
const f = fs.readFileSync(urlp); | |
response.writeHead(200, {'Content-Type':'text/html; charset=utf-8'}); | |
response.write(f); | |
} catch (e) { | |
response.writeHead(404, {'Content-Type':'text/html; charset=utf-8'}); | |
response.write(fs.readFileSync("static/404.html")); | |
} | |
response.end(); | |
} | |
}); | |
// (後略) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment