1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

JavaScriptでのサーバ構築がしたい!

Last updated at Posted at 2025-12-15

はじめに

開発をしている中で、「あ、これ知らなかったな」ということに遭遇することがあります。
今回はその一つについて、備忘録も兼ねてアウトプットとしてまとめておきます。

サーバを作りたい!

サーバを作るパッケージとしては http というのを用いるのがいいらしいです。
https でもいいのですが、(無料でできるそうですが)署名云々が面倒なのでみなかったことにします。

まず、サーバを作ります。
res.end("Hello World!"); の場所にはサーバの応答等がかけます。
今回は Hello World! と表示されるだけですが。

import http from "http"; //インポートを忘れずに
const server = http.createServer((req, res) => {
  res.end("Hello World!");
});

これだけでは応答システムを設定しただけなのでportを開きます。

server.listen("8080", () => {
  console.log(
    `Server at http://localhost:8080`
  );
});

これで、ローカルにサーバが立ったはずです。
ブラウザで開いてみれば Hello World! が見えるでしょう。

なお、サーバーは以下のコードで閉じられます。

server.close();

まとめ (学んだこと)

creatだけではサーバを動かすことはできない。

1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?