LoginSignup
1
1

More than 3 years have passed since last update.

Node.js のWebサーバーをWindowsで作る ・環境構築

Last updated at Posted at 2019-10-16

Node.jsのWebサーバーをwindows環境で作ってみた

Node.jsの知識が必要になったため、windowsでの環境構築の方法をまとめてみました。

今回はversion8系を使っていましたので以下のコードを参照しました。
最新版の11系においてもメソッドの変更等は無いようなので、
そのまま利用可能です。

Webサーバー

server.js
const http = require('http');

const hostname = '127.0.0.1'; 

const port = 3000;
//空いているポート番号を適当に

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello, World!\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

サーバーを立ち上げる

server.jsの保存をしたらファイルを実行

node server.js

以下のように表示が出力されます。

Server running at http://127.0.0.1:3000/

ブラウザで動作確認

そうしたら、ブラウザで
http://127.0.0.1:3000/
にアクセスをすると、Hello World!が表示されるはずです。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1