forked from torusresearch/torus-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
28 lines (23 loc) · 871 Bytes
/
server.js
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
/* eslint-disable import/no-extraneous-dependencies */
const express = require('express')
const send = require('send')
const fs = require('fs')
const log = require('loglevel')
const path = require('path')
log.enableAll()
const app = express()
const fileExtensionRegexp = /[^/?]+\\.[^/]+$/
const versionRegex = /^\/v\d+\.\d+\.\d+\//
app.use('*', (req, res) => {
let finalPath = req.params[0].replace(versionRegex, '')
log.info('path', finalPath, req.params[0])
if (!finalPath.match(fileExtensionRegexp)) {
finalPath = 'index.html'
}
log.info('path2', finalPath, req.params[0])
const dirPath = path.resolve(path.join(__dirname, 'dist', finalPath))
log.info('dir', dirPath)
if (!fs.existsSync(dirPath)) return res.status(404).text('not found')
return send(req, dirPath).pipe(res)
})
app.listen(4050, () => log.info('server running on port 4050'))