Help us understand the problem. What is going on with this article?

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

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

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

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

https://nodejs.org/docs/latest-v8.x/api/synopsis.html

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!が表示されるはずです。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away