MCPcopy
hub / github.com/tinyhttp/tinyhttp / redirect

Function redirect

packages/res/src/redirect.ts:12–47  ·  view source on GitHub ↗
(
  req: Request,
  res: Response,
  next: Next
)

Source from the content-addressed store, hash-verified

10type next = (err?: any) => void
11
12export 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}

Callers 2

extendMiddlewareFunction · 0.90
res.test.tsFile · 0.90

Calls 2

setLocationHeaderFunction · 0.90
formatResponseFunction · 0.90

Tested by

no test coverage detected