-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev-server.js
More file actions
55 lines (45 loc) · 1.76 KB
/
Copy pathdev-server.js
File metadata and controls
55 lines (45 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import express from 'express'
import fs from "fs"
import path from 'path'
import buildPage from './build/build-page.js'
import buildRoot from './build/build-root.js'
buildRoot()
const app = express()
const port = 8000
app.use(function (req, res, next) {
if (req.path.substr(-1) == '/' && req.path.length > 1) {
let query = req.url.slice(req.path.length)
res.redirect(301, req.path.slice(0, -1) + query)
} else {
next()
}
})
app.use('/*.js', async (req, res, next) => {
let url = req.originalUrl.split('?')[0]
if (url.includes('/assets/'))
return next()
let filePath = `./src/pages${url}`
if (filePath.endsWith('/'))
filePath = filePath.slice(0, -1)
res.sendFile(path.resolve(filePath.replace('./src/pages/', './docs/')))
})
app.use('/**', async (req, res, next) => {
let url = req.originalUrl.split('?')[0]
if (url.includes('/assets/') || url.endsWith('.js') || url.endsWith('.js/') || url.endsWith('.png/') || url.endsWith('.png') || url.endsWith('.xml/') || url.endsWith('.ico') || url.endsWith('.ico/') || url.endsWith('.svg/') || url.endsWith('.webmanifest/') || url.endsWith('.webmanifest') || url.endsWith('.css/') || url.endsWith('.css') || url.endsWith('.txt') || url.endsWith('.txt/'))
return next()
url = url === '/' ? '/index' : url
if (url.endsWith('/'))
url = url.slice(0, -1)
const filePath = `./src/pages${url}.js`
console.log(`building page ${filePath}`)
await buildPage(filePath)
res.sendFile(path.resolve(filePath.replace('./src/pages/', './docs/').replace('.js', '.html')))
})
app.use((req, res, next) => {
console.log(req.originalUrl)
next()
})
app.use(express.static('docs', {extensions:['html', 'css'], redirect:false}))
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})