tech.chakapoko.com
Home / Node.js / HTTP

[Node.js]HTTPサーバを起動する

http.createServer を使うと簡単にHTTPサーバを起動できます。

次のコードは8000番ポートでHTTPサーバーを起動し、全てのリクエストに対してHello, Worldというメッセージを返します。

const http = require('http');

http.createServer((req, res) => {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
}).listen(8000, () => {
    console.log('Server running')
});

Related contents