이 방법에서는 HTTP 요청 헤더에 X-API-Version
과 같은 커스텀 헤더를 사용하여 API 버전을 지정합니다.
요청:
GET /resources HTTP/1.1
Host: api.example.com
X-API-Version: 2
서버에서 버전 확인:
서버에서는 이 헤더를 확인하고 해당 버전에 맞는 처리를 수행합니다.
const express = require('express');
const app = express();
app.get('/resources', (req, res) => {
const apiVersion = req.headers['x-api-version'];
if (apiVersion === '1') {
// v1 처리 로직
res.send('Response from API version 1');
} else if (apiVersion === '2') {
// v2 처리 로직
res.send('Response from API version 2');
} else {
res.status(400).send('Invalid API version');
}
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
이 방법에서는 HTTP 요청의 Accept
헤더를 사용하여 클라이언트가 원하는 API 버전을 지정합니다.
요청:
GET /resources HTTP/1.1
Host: api.example.com
Accept: application/vnd.example.v2+json
서버에서 버전 확인:
서버에서는 Accept
헤더를 확인하고 해당 버전에 맞는 처리를 수행합니다.