( req: Request, res: Response, next: Next )
| 10 | type next = (err?: any) => void |
| 11 | |
| 12 | export const redirect = <Request extends Req = Req, Response extends Res = Res, Next extends next = next>( |
| 13 | req: Request, |
| 14 | res: Response, |
| 15 | next: Next |
| 16 | ) => (url: string, status?: number) => { |
| 17 | let address = url |
| 18 | status = status || 302 |
| 19 | |
| 20 | let body = '' |
| 21 | |
| 22 | address = setLocationHeader(req, res)(address).getHeader('Location') as string |
| 23 | |
| 24 | formatResponse( |
| 25 | req, |
| 26 | res, |
| 27 | next |
| 28 | )({ |
| 29 | text: () => { |
| 30 | body = STATUS_CODES[status] + '. Redirecting to ' + address |
| 31 | }, |
| 32 | html: () => { |
| 33 | const u = escapeHtml(address) |
| 34 | |
| 35 | body = `<p>${STATUS_CODES[status]}. Redirecting to <a href="${u}">${u}</a></p>` |
| 36 | } |
| 37 | }) |
| 38 | |
| 39 | res.setHeader('Content-Length', Buffer.byteLength(body)) |
| 40 | |
| 41 | res.statusCode = status |
| 42 | |
| 43 | if (req.method === 'HEAD') res.end() |
| 44 | else res.end(body) |
| 45 | |
| 46 | return res |
| 47 | } |
no test coverage detected