NodeJs Router

Node.js Router 알아보기.

라우팅이란

라우팅은 애플리케이션 엔드 포인트(URI)의 정의, 그리고 URI가 클라이언트 요청에 응답하는 방식을 의미하고 있습니다.
예를 들어 다음과 같은 라우터가 있다면

1
2
3
4
5
6
7
const express = require('express');
const app = express();

// respond with "hello world" when a GET request is made to the homepage
app.get('/', function(req, res) {
res.send('hello world');
});

루트 “/“ 엔트포인트로 접근 시 hello word라는 문자열을 브라우저에 띄워 줄 것 입니다.

라우트 메소드

라우트 메소드는 HTTP 메소드 중 하나로부터 파생되며, express 클래스의 인스턴스에 연결됩니다.

다음 코드는 앱의 루트에 대한 GET 및 POST 메소드에 대해 정의된 라우트의 예입니다.

1
2
3
4
5
6
7
8
9
// GET method route
app.get('/', function (req, res) {
res.send('GET request to the homepage');
});

// POST method route
app.post('/', function (req, res) {
res.send('POST request to the homepage');
});

express는 HTTP메서드에 해당하는 다음과 같은 목록에 해당하는 메서드를 지원합니다.
get, post, put, head, delete, options, trace, copy, lock, mkcol, move, purge, propfind, proppatch, unlock, report, mkactivity, checkout, merge, m-search, notify, subscribe, unsubscribe, patch, search 및 connect.

다양한 HTTP메서드를 사용하여 express에서 라우팅을 생성 할 수 있다.

Author

han Ju Ryeon

Posted on

2021-09-09

Updated on

2021-12-05

Licensed under

댓글