// From https://gist.github.com/186526/82b7372619fb4b029568040ee4a4aa44, Open source with MIT license import { response, request } from '../index'; import { defaultHeaders } from '../src/interface/response'; function uuid(): string { const s: any[] = []; const hexDigits = '0123456789abcdef'; for (let i = 0; i < 36; i++) { s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1); } s[14] = '4'; // bits 12-15 of the time_hi_and_version field to 0010 s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01 s[8] = s[13] = s[18] = s[23] = '-'; const uuid = s.join(''); return uuid; } const codeAlternative: { [index: number]: string } = { 100: 'Continue', 101: 'Switching Protocols', 102: 'Processing', 103: 'Early Hints', 200: 'OK', 201: 'Created', 202: 'Accepted', 203: 'Non-Authoritative Information', 204: 'No Content', 205: 'Reset Content', 206: 'Partial Content', 207: 'Multi Status', 208: 'Already Reported', 226: 'IM Used', 300: 'Multiple Choices', 301: 'Moved Permanently', 302: 'Found', 303: 'See Other', 304: 'Not Modified', 305: 'Use Proxy', 306: 'Switch Proxy', 307: 'Temporary Redirect', 308: 'Permanent Redirect', 400: 'Bad Request', 401: 'Unauthorized', 402: 'Payment Required', 403: 'Forbidden', 404: 'Not Found', 405: 'Method Not Allowed', 406: 'Not Acceptable', 407: 'Proxy Authentication Required', 408: 'Request Time-out', 409: 'Conflict', 410: 'Gone', 411: 'Length Required', 412: 'Precondition Failed', 413: 'Request Entity Too Large', 414: 'Request-URI Too Large', 415: 'Unsupported Media Type', 416: 'Requested Range not Satisfiable', 417: 'Expectation Failed', 418: "I'm a teapot", 421: 'Misdirected Request', 422: 'Unprocessable Entity', 423: 'Locked', 424: 'Failed Dependency', 426: 'Upgrade Required', 428: 'Precondition Required', // RFC 6585 429: 'Too Many Requests', 431: 'Request Header Fields Too Large', // RFC 6585 451: 'Unavailable For Legal Reasons', 500: 'Internal Server Error', 501: 'Not Implemented', 502: 'Bad Gateway', 503: 'Service Unavailable', 504: 'Gateway Time-out', 505: 'HTTP Version not Supported', 506: 'Variant Also Negotiates', 507: 'Insufficient Storage', 508: 'Loop Detected', 510: 'Not Extended', 511: 'Network Authentication Required', }; const template: string = '${statusCode} | ${codeAlternative}web
Your Client
${clientStatus}
alt_route
Handlers.js
Working
widgets
Route
${ServerStatus}
${codeAlternative}

${explain}

What can I do?

${howto}

'; export default ( errorCode: number, errorMessage: string = `The route you are trying access is error.`, ) => async (request: request) => { return new response( template .replaceAll('${statusCode}', errorCode.toString()) .replaceAll('${codeAlternative}', codeAlternative[errorCode]) .replaceAll('${explain}', errorMessage) .replaceAll( '${howto}', `You can go to the homepage of the website.`, ) .replaceAll('${id}', uuid()) .replaceAll( '${ServerStatus}', errorCode >= 500 ? 'Error' : errorCode <= 500 && errorCode >= 400 ? `Working but ${codeAlternative[errorCode]}` : 'Working', ) .replaceAll( '${ServerStatusLowerCase}', errorCode >= 500 ? 'error' : errorCode <= 500 && errorCode >= 400 ? `working-with-error` : 'working', ) .replaceAll('${ip}', request.ip) .replaceAll( '${clientStatus}', errorCode <= 500 && errorCode >= 400 ? `Working but receive ${codeAlternative[errorCode]}` : 'Working', ) .replaceAll( '${clientStatusShort}', errorCode <= 500 && errorCode >= 400 ? `working-with-error` : 'working', ), errorCode, (() => { const Headers = new defaultHeaders(); Headers.set('Content-Type', 'text/html; charset=utf-8'); return Headers; })(), ); };