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