mirror of
https://github.com/nolanlawson/pinafore
synced 2024-12-11 09:45:24 +01:00
fabf9399f2
* fix: use static server for `node server.js` Aligns the Node server with the Zeit server. Fixes #1019 * fix test
43 lines
1.1 KiB
JavaScript
Executable File
43 lines
1.1 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
const path = require('path')
|
|
const express = require('express')
|
|
const compression = require('compression')
|
|
const { routes: nowRoutes } = require('./now.json')
|
|
|
|
const { PORT = 4002 } = process.env
|
|
const app = express()
|
|
const exportDir = path.resolve(__dirname, '__sapper__/export')
|
|
|
|
const routes = nowRoutes.map(({ src, headers, dest }) => ({
|
|
regex: new RegExp(src),
|
|
headers,
|
|
dest
|
|
}))
|
|
|
|
app.use(compression({ threshold: 0 }))
|
|
|
|
app.use(express.static(exportDir, {
|
|
setHeaders (res, thisPath) {
|
|
let localPath = '/' + path.relative(exportDir, thisPath)
|
|
for (let { regex, headers } of routes) {
|
|
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))
|