-
Notifications
You must be signed in to change notification settings - Fork 7
/
server.js
41 lines (32 loc) · 905 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
29
30
31
32
33
34
35
36
37
38
39
40
41
const express = require('express')
const port = Number(process.env.PORT) || 5001
const env = require('lil-env-thing')
const cleanDeep = require('clean-deep')
const getRepo = require('./lib/get-repo')
const app = express()
// Middleware
app.use(express.static(__dirname))
app.get('/', require('./lib/home'))
app.get('/*', redirectToRepo)
function redirectToRepo (req, res, next) {
if (req.path === '/favicon.ico') return next()
const packageName = req.path.replace(/^\//, '')
const data = cleanDeep({
name: packageName,
domain: req.domain,
ip: req.ip,
headers: req.headers,
date: new Date()
})
// don't pollute test and dev output
if (env.production) {
console.log(`query: ${JSON.stringify(data)}`)
}
res.redirect(getRepo(packageName))
}
if (!module.parent) {
app.listen(port, () => {
console.log(`app running on ${port}`)
})
}
module.exports = app