LoginSignup
0

More than 1 year has passed since last update.

(入門)Node.jsで初めてのWebサーバーを立ち上げる Windoows10環境

Last updated at Posted at 2021-05-11

node.js で サーバーを作成します。

環境
node v12.18.1 npm 6.14.4
windows10

nodeは以下のサイトを参考にして、インストールします。
https://qiita.com/satoyan419/items/56e0b5f35912b9374305

nodeのバージョン管理は以下のサイトを参考にします。
https://qiita.com/satoyan419/items/56e0b5f35912b9374305

vscodeでフォルダを作成して、
app.jsファイルに以下のコードを記入します。

app.js
// モジュールをロードする DOMの代わり
const http = require('http');


// サーバーを作る
var server = http.createServer(
  (request,response)=>{
    response.end('hello');
  }
);
// ポート番号:3000で受け付け開始
server.listen(port =3000);

最後のserver.listen(port =3000);

のportの部分がないと

エラー
code: 'MODULE_NOT_FOUND', requireStack: []
が出きたので忘れないでください。

node.jsでは、javascriptで使用していたDOM要素の代わりに、
モジュールというものをロードします。

コマンドプロンプトを開いて、

コマンドプロンプト(cmd)

node app.js

localhost:3000にブラウザでアクセスしてください。

公式ドキュメントでも同様なコードが書いてあるので、参考に。
https://nodejs.org/ja/docs/guides/getting-started-guide/

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
0