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

Your email address will not be published. Required fields are marked *