2019-02-13 07:12:50 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2019-02-24 00:09:48 +00:00
|
|
|
const path = require('path')
|
|
|
|
const express = require('express')
|
|
|
|
const compression = require('compression')
|
2020-08-26 00:45:53 +01:00
|
|
|
const { routes: rawRoutes } = require('./vercel.json')
|
2019-02-13 07:12:50 +00:00
|
|
|
|
2019-02-24 00:09:48 +00:00
|
|
|
const { PORT = 4002 } = process.env
|
|
|
|
const app = express()
|
|
|
|
const exportDir = path.resolve(__dirname, '__sapper__/export')
|
|
|
|
|
2020-08-26 00:45:53 +01:00
|
|
|
const routes = rawRoutes.map(({ src, headers, dest }) => ({
|
2019-02-24 00:09:48 +00:00
|
|
|
regex: new RegExp(src),
|
|
|
|
headers,
|
|
|
|
dest
|
|
|
|
}))
|
|
|
|
|
|
|
|
app.use(compression({ threshold: 0 }))
|
|
|
|
|
|
|
|
app.use(express.static(exportDir, {
|
|
|
|
setHeaders (res, thisPath) {
|
2019-08-03 21:49:37 +01:00
|
|
|
const localPath = '/' + path.relative(exportDir, thisPath)
|
|
|
|
for (const { regex, headers } of routes) {
|
2019-02-24 00:09:48 +00:00
|
|
|
if (regex.test(localPath)) {
|
|
|
|
res.set(headers)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
routes.forEach(({ regex, headers, dest }) => {
|
|
|
|
app.get(regex, (req, res) => {
|
|
|
|
res.set(headers)
|
|
|
|
res.sendFile(path.resolve(exportDir, dest ? req.path.replace(regex, dest) : req.path))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
app.listen(PORT, () => console.log(`listening on port ${PORT}`))
|
|
|
|
|
|
|
|
// Handle SIGINT (source: https://git.io/vhJgF)
|
|
|
|
process.on('SIGINT', () => process.exit(0))
|