Skip to content

Instantly share code, notes, and snippets.

@TakizawaAkira
Created June 24, 2017 12:30
Show Gist options
  • Save TakizawaAkira/3fdfd39efbd1a26c6f57c1ddc92b4b88 to your computer and use it in GitHub Desktop.
Save TakizawaAkira/3fdfd39efbd1a26c6f57c1ddc92b4b88 to your computer and use it in GitHub Desktop.
node-server
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) {
// ファイルを読み込んだら、コールバック関数を実行する。
fs.readFile('./index.html', '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