Skip to content

Instantly share code, notes, and snippets.

@pandanote-info
Created July 24, 2020 06:58
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 pandanote-info/7aed8792bda7bd93c49541aad9f2f2de to your computer and use it in GitHub Desktop.
Save pandanote-info/7aed8792bda7bd93c49541aad9f2f2de to your computer and use it in GitHub Desktop.
Node.js用staticなHTMLファイルの送信用コード例
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