0
0

More than 1 year has passed since last update.

Node.js忘備録 ~WEBサーバーの建て方

Last updated at Posted at 2022-09-19

NodeJSを用いたWEBサーバーの作り方について忘備録です。

環境について

Replit.comというオンラインIDEを用いています。
全ての言語使えるので便利です。
https://replit.com

WEBサーバー

ファイル構成

|
|ーーindex.js
|ーーindex.html

コード (index.js)

//---------------------------------
// HTTPサーバー (express)
//---------------------------------
// ライブラリインポート
const express = require('express');
const app = express();
const http = require('http').createServer(app);

// "/"にアクセスがあったらindex.htmlを返却
app.use('/', express.static(__dirname + '/'));
app.get("/", (req, res)=>{
  res.sendFile(__dirname + "/index.html");
});

// 3000番でサーバーを起動する
http.listen(3000, ()=>{
  console.log("listening on :3000");
});

同じ階層のindex.htmlを返します。
index.htmlの中身は好きに書いてください:angel:

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