NODEJS๊ธฐ๋ณธ http server ,NODEJS Basic http server(npm express,routing)

express ๋ชจ๋“ˆ์€ ์ปค๋งจ๋“œ๋ผ์ธ์—์„œ npm ๋ช…๋ น์–ด๋กœ ์„ค์น˜ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๋ธŒ๋ผ์šฐ์ € ์ฃผ์†Œ๋ฅผ ํ†ตํ•ด์„œ ์›น์‚ฌ์ดํŠธ์— ์ ‘์† ํ•  ๋•Œ ๋„๋ฉ”์ธ ์ฃผ์†Œ ์—”๋“œ ํฌ์ธํŠธ์— ๋”ฐ๋ผ ์›น์„œ๋ฒ„๋Š” ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๋‹ค๋ฅธ ๋‚ด์šฉ์„ ๋ณด์—ฌ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด์„œ http://freelifemakers.org/wordpress ๋ผ๊ณ  ํ•œ๋‹ค๋ฉด ์ด ‘/wordpress’ ๋ถ€๋ถ„์„ ์—”๋“œํฌ์ธํŠธ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
๋‹ค๋ฅธ ๋ง๋กœ ๋ผ์šฐํŠธ ๋˜๋Š” ๋ฃจํŠธ๋ผ๊ณ  ์ •์˜ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
์ด๋ ‡๊ฒŒ ์—”๋“œํฌ์ธํŠธ์— ๋”ฐ๋ผ ํด๋ผ์ด์–ธํŠธ์— ๋‹ค๋ฅธ ์‘๋‹ต์„ ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๋ผ์šฐํŒ…์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
express๋Š” ์ด๋ ‡๊ฒŒ ๋ผ์šฐํŒ… ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๋ชจ๋“ˆ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

The express module can be installed with the npm command from the command line.
When you access a website through a browser address, depending on the domain address endpoint, the web server can show the client different content.
For example, if it’s http://freelifemakers.org/wordpress , this ‘/wordpress’ part is called an endpoint.
In other words, it can be defined as a route.
The ability to respond differently to clients depending on the endpoint is called routing.
You can think of express as a module that allows you to use routing capabilities like this.

1.์„ค์น˜๋ฐฉ๋ฒ•(Installation)
-jsํŒŒ์ผ ๋””๋ ‰ํ† ๋ฆฌ๋กœ ์ด๋™ํ•ด์„œ ์ปค๋ฉ˜๋“œ๋ผ์ธ์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.
-Go to the js file directory and enter the following on the command line.


#npm install express

2.๋ผ์šฐํŒ…(routing)
-๋…ธ๋ž€์ƒ‰,๋…น์ƒ‰,ํŒŒ๋ž€์ƒ‰ ๋ถ€๋ถ„์€ ๋ชจ๋‘ express๋ฅผ ์œ„ํ•œ ์ฝ”๋“œ ์ž…๋‹ˆ๋‹ค.
-๋…ธ๋ž€์ƒ‰์€ ๋ชจ๋“ˆ ํฌํ•จ ์‹œํ‚ค๋Š” ๋ถ€๋ถ„์ด๊ณ ,๋…น์ƒ‰๊ณผ ํŒŒ๋ž‘ ์ƒ‰์€ get๋ฐฉ์‹(๋ธŒ๋ผ์šฐ์ € ์ฃผ์†Œํ‘œ์‹œ์ค„)์„ ํ†ตํ•œ ์ ‘๊ทผ์— ๋Œ€ํ•œ ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค.
-๋…ธ๋ž€์ƒ‰ ํ™”์‚ดํ‘œํ•จ์ˆ˜์™€ ํ•˜๋Š˜์ƒ‰ ๊ธฐ์กด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ•จ์ˆ˜๊ฐ€ ๋ชจ๋‘ ์‚ฌ์šฉ ๊ฐ€๋Šฅ ํ•ฉ๋‹ˆ๋‹ค.

-The yellow, green, and blue parts are all codes for express.
-The yellow color is the part that includes the module, and the green and blue colors are the part for access through the get method (browser address bar).
-Both yellow arrow functions and light blue existing JavaScript type functions are available.


1)app.get(‘/’, (req, res) => {}
http://freelifemakers.org ๋˜๋Š” http://localhost ์ฒ˜๋Ÿผ ์ ‘์†ํ–ˆ์„ ๋•Œ ๋ณด์—ฌ์ค„ ๋‚ด์šฉ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
Set the content to be displayed when accessed, such as http://freelifemakers.org or http://localhost.

2)app.get(‘/about’,(req, res) => {}
http://freelifemakers.org/about ๋˜๋Š” http://localhost/about ์ฒ˜๋Ÿผ ์ ‘์†ํ–ˆ์„ ๋•Œ ๋ณด์—ฌ์ค„ ๋‚ด์šฉ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
Set the content to be displayed when accessed, such as http://freelifemakers.org/about or http://localhost/about.

3)app.get(‘/random.text’, function (req, res) {}
http://freelifemakers.org/random.text ๋˜๋Š” http://localhost/random.text ์ฒ˜๋Ÿผ ์ ‘์†ํ–ˆ์„ ๋•Œ ๋ณด์—ฌ์ค„ ๋‚ด์šฉ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
Set the content to be displayed when accessed, such as http://freelifemakers.org/random.text or http://localhost/random.text.

3.์ „์ฒด์ฝ”๋“œ(full code)

//server_ex.js
const http = require('http');
const express = require('express');
const app = express();
const server = http.createServer(app);

//routing
app.get('/',(req, res) => {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Express Hello World!\n');
});

app.get('/about',(req, res) => {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Express About!\n');
});  

app.get('/random.text', function (req, res) {
    res.send('Express random.text');
});

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

Leave a Reply