1 | 'use strict'
|
2 |
|
3 | const mime = require('../detect/mime')
|
4 | const interpolate = require('../interpolate')
|
5 | const textMimeType = mime.getType('text')
|
6 | const byStatus = {
|
7 | 403: 'Forbidden',
|
8 | 404: 'Not found',
|
9 | 405: 'Method Not Allowed',
|
10 | 500: 'Internal Server Error',
|
11 | 501: 'Not Implemented',
|
12 | 508: 'Loop Detected'
|
13 | }
|
14 |
|
15 | module.exports = {
|
16 | schema: {
|
17 | status: 'number',
|
18 | headers: {
|
19 | type: 'object',
|
20 | defaultValue: {}
|
21 | }
|
22 | },
|
23 | redirect: async function ({ mapping, match, response, redirect }) {
|
24 | const statusCode = redirect
|
25 | const content = byStatus[statusCode] || ''
|
26 | const length = content.length
|
27 | const headers = mapping ? mapping.headers : undefined
|
28 | response.writeHead(statusCode, {
|
29 | 'Content-Type': textMimeType,
|
30 | 'Content-Length': length,
|
31 | ...interpolate(match, headers)
|
32 | })
|
33 | response.end(content)
|
34 | }
|
35 | }
|