Express를 통한 라우팅에 대한 추가 정보
6906 단어 programmingjavascriptwebdevnode
지금 http://jauyeung.net/subscribe/에서 내 이메일 목록을 구독하십시오.
라우팅은 백엔드 애플리케이션에서 가장 중요한 부분입니다. Express를 사용하면 URL을 경로 처리기 코드로 쉽게 라우팅할 수 있습니다.
이 기사에서는 Express를 사용하여 다양한 종류의 경로를 만드는 방법을 살펴보겠습니다.
모든 요청 처리
app.all 메서드를 사용하여 모든 요청 메서드를 처리할 수 있습니다. 이를 사용하기 위해 다음과 같이 작성할 수 있습니다.
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.all('*', (req, res, next) => {
console.log('route called');
next();
})
app.get('/', (req, res) => {
res.send('hi');
})
app.listen(3000, () => console.log('server started'));
위의 app.all 호출은 메서드나 URL이 전송될 때 호출됩니다. 로그'route called'한 다음next를 호출하여 해당 경로에 대한 특정 경로 핸들러를 호출합니다.
문자가 아닌 기호
Express는 문자가 아닌 기호가 있는 경로를 일치시킬 수 있습니다. 예를 들어 다음이 있는 경우:
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get('/foo.bar', (req, res) => {
res.send('hi');
})
app.listen(3000, () => console.log('server started'));
그런 다음 foo.bar 에 GET 요청을 하면 hi 를 얻습니다.
특수 문자
선택적 문자
?가 URL 경로에 있으면 선택적 문자로 간주됩니다. 예를 들어, ab?c는 ac 또는 abc와 일치합니다.
예를 들어 다음이 있는 경우:
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get('/ab?c', (req, res) => {
res.send('hi');
})
app.listen(3000, () => console.log('server started'));
그런 다음 /ac 또는 /abc 에 요청하면 hi 를 얻습니다.
하나 이상의 문자
URL에 + 기호가 있으면 그 앞에 오는 하나 이상의 문자와 일치합니다.
예를 들어 다음이 있는 경우:
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get('/ab+c', (req, res) => {
res.send('hi');
})
app.listen(3000, () => console.log('server started'));
그런 다음 /abc , /abbc , /abbbc 등에 요청할 때 hi 를 얻습니다.
와일드카드
URL의 *는 와일드카드 문자입니다.
예를 들어 다음이 있는 경우:
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get('/ab\*c', (req, res) => {
res.send('hi');
})
app.listen(3000, () => console.log('server started'));
그런 다음 URL에 ab와 c 사이에 아무 것도 입력하고 hi 응답을 얻습니다.
선택적 문자 그룹
문자 그룹은 괄호로 묶이고 뒤에 ?가 있는 경우 선택 사항입니다.
예를 들어 다음이 있는 경우:
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get('/ab(cd)?ef', (req, res) => {
res.send('hi');
})
app.listen(3000, () => console.log('server started'));
그런 다음 /abcdef 또는 /abef 로 이동하면 hi 를 얻습니다.
정규식
정규식을 경로 경로로 사용할 수도 있습니다. 예를 들어 다음이 있는 경우:
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get(/\/a/, (req, res) => {
res.send('hi');
})
app.listen(3000, () => console.log('server started'));
그런 다음 /a 에 요청하면 hi 를 얻습니다.
hi 로 끝나는 경로로 요청을 할 때 foo 를 얻으려면 다음과 같이 작성할 수 있습니다.
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get(/\/.\*foo$/, (req, res) => {
res.send('hi');
})
app.listen(3000, () => console.log('server started'));
경로 매개변수
매개변수의 키 이름 앞에 콜론을 작성하여 매개변수를 지정할 수 있습니다.
예를 들어 다음과 같이 작성할 수 있습니다.
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get('/:name/:age', (req, res) => {
res.send(req.params);
})
app.listen(3000, () => console.log('server started'));
그러면 다음과 같은 응답을 받습니다.
{"name":"Mary","age":"10"}
:name 및 :age는 경로 자리 표시자로 해석됩니다. req.params 콜론 없이 키로 사용합니다. 그러면 자리 표시자 대신 설정되는 것이 해당 값이 됩니다.
경로 처리기
위 app.get의 두 번째 인수에 있는 콜백은 경로 처리기입니다. POST, PUT 및 DELETE 요청에 대해서도 동일한 작업을 수행할 수 있습니다.
app.get 함수와 함께 여러 핸들러를 연결할 수 있습니다. 예를 들어 다음과 같이 작성할 수 있습니다.
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get('/',
(req, res, next) => {
console.log('handler 1 called');
next()
},
(req, res) => {
console.log('handler 2 called');
res.send();
})
app.listen(3000, () => console.log('server started'));
그러면 다음과 같이 표시됩니다.
handler 1 called
handler 2 called
app.post 출력에서.
다음 경로 핸들러 함수를 호출하기 위해 경로 핸들러의 매개변수에서 발생하는 app.put 함수를 호출합니다.
결론
Express를 사용한 라우팅은 간단합니다. 문자열이나 정규식을 사용하여 경로 경로를 지정할 수 있습니다.
app.delete , next 또는 console.log와 같은 문자열 경로의 일부 문자는 선택적 문자 또는 와일드카드가 있는 경로의 특수 문자입니다.
경로 매개변수의 자리 표시자는 자리 표시자 이름 앞에 콜론을 사용하여 지정합니다.
여러 경로 처리기를 함께 연결하기 위해 next를 호출합니다.
Reference
이 문제에 관하여(Express를 통한 라우팅에 대한 추가 정보), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://dev.to/aumayeung/more-about-routing-with-express-co1
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.all('*', (req, res, next) => {
console.log('route called');
next();
})
app.get('/', (req, res) => {
res.send('hi');
})
app.listen(3000, () => console.log('server started'));
Express는 문자가 아닌 기호가 있는 경로를 일치시킬 수 있습니다. 예를 들어 다음이 있는 경우:
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get('/foo.bar', (req, res) => {
res.send('hi');
})
app.listen(3000, () => console.log('server started'));
그런 다음
foo.bar 에 GET 요청을 하면 hi 를 얻습니다.특수 문자
선택적 문자
?가 URL 경로에 있으면 선택적 문자로 간주됩니다. 예를 들어, ab?c는 ac 또는 abc와 일치합니다.
예를 들어 다음이 있는 경우:
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get('/ab?c', (req, res) => {
res.send('hi');
})
app.listen(3000, () => console.log('server started'));
그런 다음 /ac 또는 /abc 에 요청하면 hi 를 얻습니다.
하나 이상의 문자
URL에 + 기호가 있으면 그 앞에 오는 하나 이상의 문자와 일치합니다.
예를 들어 다음이 있는 경우:
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get('/ab+c', (req, res) => {
res.send('hi');
})
app.listen(3000, () => console.log('server started'));
그런 다음 /abc , /abbc , /abbbc 등에 요청할 때 hi 를 얻습니다.
와일드카드
URL의 *는 와일드카드 문자입니다.
예를 들어 다음이 있는 경우:
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get('/ab\*c', (req, res) => {
res.send('hi');
})
app.listen(3000, () => console.log('server started'));
그런 다음 URL에 ab와 c 사이에 아무 것도 입력하고 hi 응답을 얻습니다.
선택적 문자 그룹
문자 그룹은 괄호로 묶이고 뒤에 ?가 있는 경우 선택 사항입니다.
예를 들어 다음이 있는 경우:
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get('/ab(cd)?ef', (req, res) => {
res.send('hi');
})
app.listen(3000, () => console.log('server started'));
그런 다음 /abcdef 또는 /abef 로 이동하면 hi 를 얻습니다.
정규식
정규식을 경로 경로로 사용할 수도 있습니다. 예를 들어 다음이 있는 경우:
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get(/\/a/, (req, res) => {
res.send('hi');
})
app.listen(3000, () => console.log('server started'));
그런 다음 /a 에 요청하면 hi 를 얻습니다.
hi 로 끝나는 경로로 요청을 할 때 foo 를 얻으려면 다음과 같이 작성할 수 있습니다.
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get(/\/.\*foo$/, (req, res) => {
res.send('hi');
})
app.listen(3000, () => console.log('server started'));
경로 매개변수
매개변수의 키 이름 앞에 콜론을 작성하여 매개변수를 지정할 수 있습니다.
예를 들어 다음과 같이 작성할 수 있습니다.
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get('/:name/:age', (req, res) => {
res.send(req.params);
})
app.listen(3000, () => console.log('server started'));
그러면 다음과 같은 응답을 받습니다.
{"name":"Mary","age":"10"}
:name 및 :age는 경로 자리 표시자로 해석됩니다. req.params 콜론 없이 키로 사용합니다. 그러면 자리 표시자 대신 설정되는 것이 해당 값이 됩니다.
경로 처리기
위 app.get의 두 번째 인수에 있는 콜백은 경로 처리기입니다. POST, PUT 및 DELETE 요청에 대해서도 동일한 작업을 수행할 수 있습니다.
app.get 함수와 함께 여러 핸들러를 연결할 수 있습니다. 예를 들어 다음과 같이 작성할 수 있습니다.
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get('/',
(req, res, next) => {
console.log('handler 1 called');
next()
},
(req, res) => {
console.log('handler 2 called');
res.send();
})
app.listen(3000, () => console.log('server started'));
그러면 다음과 같이 표시됩니다.
handler 1 called
handler 2 called
app.post 출력에서.
다음 경로 핸들러 함수를 호출하기 위해 경로 핸들러의 매개변수에서 발생하는 app.put 함수를 호출합니다.
결론
Express를 사용한 라우팅은 간단합니다. 문자열이나 정규식을 사용하여 경로 경로를 지정할 수 있습니다.
app.delete , next 또는 console.log와 같은 문자열 경로의 일부 문자는 선택적 문자 또는 와일드카드가 있는 경로의 특수 문자입니다.
경로 매개변수의 자리 표시자는 자리 표시자 이름 앞에 콜론을 사용하여 지정합니다.
여러 경로 처리기를 함께 연결하기 위해 next를 호출합니다.
Reference
이 문제에 관하여(Express를 통한 라우팅에 대한 추가 정보), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://dev.to/aumayeung/more-about-routing-with-express-co1
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get('/ab?c', (req, res) => {
res.send('hi');
})
app.listen(3000, () => console.log('server started'));
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get('/ab+c', (req, res) => {
res.send('hi');
})
app.listen(3000, () => console.log('server started'));
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get('/ab\*c', (req, res) => {
res.send('hi');
})
app.listen(3000, () => console.log('server started'));
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get('/ab(cd)?ef', (req, res) => {
res.send('hi');
})
app.listen(3000, () => console.log('server started'));
정규식을 경로 경로로 사용할 수도 있습니다. 예를 들어 다음이 있는 경우:
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get(/\/a/, (req, res) => {
res.send('hi');
})
app.listen(3000, () => console.log('server started'));
그런 다음
/a 에 요청하면 hi 를 얻습니다.hi 로 끝나는 경로로 요청을 할 때 foo 를 얻으려면 다음과 같이 작성할 수 있습니다.const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get(/\/.\*foo$/, (req, res) => {
res.send('hi');
})
app.listen(3000, () => console.log('server started'));
경로 매개변수
매개변수의 키 이름 앞에 콜론을 작성하여 매개변수를 지정할 수 있습니다.
예를 들어 다음과 같이 작성할 수 있습니다.
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get('/:name/:age', (req, res) => {
res.send(req.params);
})
app.listen(3000, () => console.log('server started'));
그러면 다음과 같은 응답을 받습니다.
{"name":"Mary","age":"10"}
:name 및 :age는 경로 자리 표시자로 해석됩니다. req.params 콜론 없이 키로 사용합니다. 그러면 자리 표시자 대신 설정되는 것이 해당 값이 됩니다.
경로 처리기
위 app.get의 두 번째 인수에 있는 콜백은 경로 처리기입니다. POST, PUT 및 DELETE 요청에 대해서도 동일한 작업을 수행할 수 있습니다.
app.get 함수와 함께 여러 핸들러를 연결할 수 있습니다. 예를 들어 다음과 같이 작성할 수 있습니다.
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get('/',
(req, res, next) => {
console.log('handler 1 called');
next()
},
(req, res) => {
console.log('handler 2 called');
res.send();
})
app.listen(3000, () => console.log('server started'));
그러면 다음과 같이 표시됩니다.
handler 1 called
handler 2 called
app.post 출력에서.
다음 경로 핸들러 함수를 호출하기 위해 경로 핸들러의 매개변수에서 발생하는 app.put 함수를 호출합니다.
결론
Express를 사용한 라우팅은 간단합니다. 문자열이나 정규식을 사용하여 경로 경로를 지정할 수 있습니다.
app.delete , next 또는 console.log와 같은 문자열 경로의 일부 문자는 선택적 문자 또는 와일드카드가 있는 경로의 특수 문자입니다.
경로 매개변수의 자리 표시자는 자리 표시자 이름 앞에 콜론을 사용하여 지정합니다.
여러 경로 처리기를 함께 연결하기 위해 next를 호출합니다.
Reference
이 문제에 관하여(Express를 통한 라우팅에 대한 추가 정보), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://dev.to/aumayeung/more-about-routing-with-express-co1
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get('/:name/:age', (req, res) => {
res.send(req.params);
})
app.listen(3000, () => console.log('server started'));
{"name":"Mary","age":"10"}
위
app.get의 두 번째 인수에 있는 콜백은 경로 처리기입니다. POST, PUT 및 DELETE 요청에 대해서도 동일한 작업을 수행할 수 있습니다.app.get 함수와 함께 여러 핸들러를 연결할 수 있습니다. 예를 들어 다음과 같이 작성할 수 있습니다.const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get('/',
(req, res, next) => {
console.log('handler 1 called');
next()
},
(req, res) => {
console.log('handler 2 called');
res.send();
})
app.listen(3000, () => console.log('server started'));
그러면 다음과 같이 표시됩니다.
handler 1 called
handler 2 called
app.post 출력에서.다음 경로 핸들러 함수를 호출하기 위해 경로 핸들러의 매개변수에서 발생하는
app.put 함수를 호출합니다.결론
Express를 사용한 라우팅은 간단합니다. 문자열이나 정규식을 사용하여 경로 경로를 지정할 수 있습니다.
app.delete , next 또는 console.log와 같은 문자열 경로의 일부 문자는 선택적 문자 또는 와일드카드가 있는 경로의 특수 문자입니다.
경로 매개변수의 자리 표시자는 자리 표시자 이름 앞에 콜론을 사용하여 지정합니다.
여러 경로 처리기를 함께 연결하기 위해 next를 호출합니다.
Reference
이 문제에 관하여(Express를 통한 라우팅에 대한 추가 정보), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://dev.to/aumayeung/more-about-routing-with-express-co1
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
Reference
이 문제에 관하여(Express를 통한 라우팅에 대한 추가 정보), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/aumayeung/more-about-routing-with-express-co1텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)