1 | const Koa = require('koa')
|
2 | const fs = require('fs')
|
3 | const path = require('path')
|
4 |
|
5 | const app = new Koa()
|
6 | app.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 |
|
28 | app.listen(3050)
|
29 | console.log('Test Server listening on port 3050')
|