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}`);
});