UNPKG

700 BJavaScriptView Raw
1const Koa = require('koa')
2const fs = require('fs')
3const path = require('path')
4
5const app = new Koa()
6app.use((ctx) => {
7 const { path: reqPath } = ctx
8 const filePath = path.join(__dirname, '..', 'demo', reqPath === '/' ? '/index.html' : reqPath)
9 const exists = fs.existsSync(filePath)
10 if (!exists) {
11 ctx.status = 404
12 return
13 }
14
15 let type = 'text'
16 if (/html/.test(filePath)) {
17 type = 'text/html'
18 } else if (/css/.test(filePath)) {
19 type = 'text/css'
20 } else if (/js/.test(filePath)) {
21 type = 'text/javascript'
22 }
23 ctx.type = type
24 ctx.status = 200
25 ctx.body = fs.createReadStream(filePath)
26})
27
28app.listen(3050)
29console.log('Test Server listening on port 3050')