NODEJS๊ธฐ๋ณธ http server ,NODEJS Basic httpserver(ESM,COMMONJS)

nodejs์—์„œ esm๊ณผ commonjs ๋ฐฉ์‹์˜ http server code์ž…๋‹ˆ๋‹ค.
nodejs๋Š” ์›น์„œ๋ฒ„(http server)๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.์ฝ”๋“œ๋กœ ์›น์„œ๋ฒ„์˜ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.
๊ธฐ๋ณธ์ ์œผ๋กœ nodejs๋Š” ํฌํŠธ๋ฒˆํ˜ธ 3000๋ฒˆ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค .
๊ทธ๋ž˜์„œ nodejsํŒจํ‚ค์ง€์ธ react๋‚˜ express๋งŒ์œผ๋กœ ์›น์‚ฌ์ดํŠธ๋ฅผ ๋งŒ๋“ ๋‹ค๋ฉด
๋ฆฌ๋ฒ„์Šค ํ”„๋ก์‹œ๊ธฐ๋Šฅ์ด ์žˆ๋Š” Nginx๊ฐ€ ์ข‹์€ ์กฐํ•ฉ์ด ๋˜๋ฆฌ๋ผ ์ƒ๊ฐ๋ฉ๋‹ˆ๋‹ค.

This is ESM and commonjs style http server code in nodejs.
nodejs does not have a web server (http server). It implements the functions of a web server through code.
By default, nodejs uses port number 3000.
So, if you create a website with only the nodejs package react or express,
I think Nginx with reverse proxy function would be a good combination.


1.ESM,COMMONJS
– nodejs์—์„œ๋Š” ๋ชจ๋“ˆ์‚ฌ์šฉ ๋ฐฉ์‹์œผ๋กœ ESM๊ณผ COMMONJS๋ผ๋Š” ๋ฐฉ์‹์ด ์žˆ์Šต๋‹ˆ๋‹ค.
– ESM์ด ๊ฐœ์„ ๋œ ๋ฐฉ์‹์ด๊ณ  COMMONJS๊ฐ€ ์ด์ „ ๋ฐฉ์‹ ์ž…๋‹ˆ๋‹ค.

In nodejs, there are two ways to use modules: ESM and COMMONJS.
ESM is an improved method and COMMONJS is the previous method.

1)ESM
– ํŒŒ์ผ ํ™•์žฅ์ž๊ฐ€ mjs์ž…๋‹ˆ๋‹ค.
– jsํŒŒ์ผ์„ esm๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ package.jsonํŒŒ์ผ์—์„œ “Type”:”module”, ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
– ์•„๋ž˜์˜ ์ฝ”๋“œ์—์„œ ๊ฐ•์กฐ๋œ ๋…น์ƒ‰๋ถ€๋ถ„์ด ESM ๋ฐฉ์‹์˜ ์ฝ”๋“œ ์ž…๋‹ˆ๋‹ค.
– ๋ณด๋ผ์ƒ‰ ์ฝ”๋“œ ๋Œ€์‹  ๋…ธ๋ž€์ƒ‰ ์ฝ”๋ฉ˜ํŠธ ์ฒ˜๋ฆฌ๋œ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค.

-The file extension is mjs.
-When using js files in esm format, add “Type”:”module”, in the package.json file.
-The green part highlighted in the code below is the ESM type code.
-You can use the yellow commented code instead of the purple code.

// server.mjs --> ESM TYPE
import { createServer } from 'node:http';

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


// starts a simple http server locally on port 3000
/*
server.listen(3000, '127.0.0.1', () => {
  console.log('ESM:Listening on 127.0.0.1:3000');
});
*/

const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
  console.log(`ESM:Server is running on port ${PORT}`);
});

-์œ„์˜ ์ฝ”๋“œ๋ฅผ server.mjsํŒŒ์ผ์ด ์•„๋‹Œ server.jsํŒŒ์ผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์›ํ•œ๋‹ค๋ฉด package.jsonํŒŒ์ผ์— ‘ “Type”:”module”, ‘์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
-package.jsonํŒŒ์ผ์€ command line์—์„œ npm๋ช…๋ น์œผ๋กœ ๋ชจ๋“ˆ์„ ์„ค์น˜ํ•˜๋ฉด ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.

-If you want to use the above code in a server.js file rather than a server.mjs file, add ‘ “Type”:”module”, ‘ to the package.json file as follows.
-The package.json file is automatically created when you install the module using the npm command from the command line.

2)COMMONJS
-ํŒŒ์ผํ™•์žฅ์ž๊ฐ€ js์ž…๋‹ˆ๋‹ค.
-nodejs์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋ณธ์ ์ธ ๋ชจ๋“ˆ ์‚ฌ์šฉ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ํŠน๋ณ„ํ•œ ์…‹ํŒ…์ด ํ•„์š” ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
-์•„๋ž˜์˜ ์ฝ”๋“œ์—์„œ ๊ฐ•์กฐ๋œ ๋…น์ƒ‰๋ถ€๋ถ„์ด COMMONJS ๋ฐฉ์‹์˜ ์ฝ”๋“œ ์ž…๋‹ˆ๋‹ค.
-๋ณด๋ผ์ƒ‰ ๋ถ€๋ถ„์€ COMMONJS๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฐ”๋€ ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค.

-The file extension is js.
-This is the basic module usage method provided by nodejs. No special settings are required.
The purple part is the part that was changed using COMMONJS.

//server.js --> Commonjs type
const http = require('http');

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


// Start the server
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
  console.log(`CommonJS:Server is running on port ${PORT}`);
});

Leave a Reply