codestates / codestates/Memory-It
[Dev Log] Express: res.location('/').status(302).end()
- Dominant language
- JavaScript
- Stars
- 1
- Forks
- 2
- PR merge metrics
- No merged PRs in 30d
Description
## 기존에 알고있던 부분
Express에서 res.location('/').status(302)를 하면 클라이언트의 url을 '/'로 이동시킨다고 생각하고 있었다.
302 또는 301상태코드로 location('/')을 하게되면 location에 해당하는 endpoint의 리소스로 연결시켜주는 역할을 한다.
**_해당기능은 SEO와 직접적인 연관이 있고 비즈니스적으로 매우유용하게 사용될 수 있다._**
**Client**
```jsx
const [ttt, setTtt] = useState('default..')
console.log(ttt)
// 버튼클릭시 onTest로 axios요청을 보낸다. 엔드포인트는 /loct 로 설정되어있음
const onTest = () => {
axios.get(LOCTEST).then(res => setTtt(res.data))
}
```
**Server**
```jsx
// 클라이언트는 /loct로 요청을 보냈으나 /subloct로 리다이렉트 하고있다.
router.get('/', (req, res) => {
res.send('//')
})
router.get('/loct', (req, res) => {
res.location('/subloct').status(302).end()
})
router.get('/subloct', (req, res) => {
res.send('migrated!!')
})
```
클라이언트에서 버튼 클릭 시 migrated!!라는 응답을 받아 볼 수 있다.
[레퍼런스 Blog](https://nsinc.tistory.com/168)
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.