| 73 | // This is for visual test cases from `npm run test:visual:copy:examples`. |
| 74 | // NOTE: `server-handler` `rewrites` does not support to visit ancestor directories. |
| 75 | function rewrite(urlPrefix) { |
| 76 | if (request.url.startsWith(urlPrefix)) { |
| 77 | // request.url is like '/xxx/yyy' |
| 78 | const filePath = path.join(ECHARTS_EXAMPLES_DIR, request.url); |
| 79 | // console.log('custom rewrite: ', filePath, request.url); |
| 80 | if (path.extname(filePath) === '.json') { |
| 81 | // Then jQuery can automatically parse JSON string to JSON object. |
| 82 | // Some cases rely on that. |
| 83 | response.setHeader('Content-Type', 'application/json'); |
| 84 | } |
| 85 | return fs.createReadStream(filePath) |
| 86 | .on('error', () => { |
| 87 | response.statusCode = 404; |
| 88 | response.end('Not found'); |
| 89 | }) |
| 90 | .pipe(response); |
| 91 | } |
| 92 | } |
| 93 | let rewriteResult; |
| 94 | if ((rewriteResult = rewrite('/public/data/asset/data')) |
| 95 | || (rewriteResult = rewrite('/e2e/e2e-asset')) |