unpkg/server.js

45 lines
1.2 KiB
JavaScript
Raw Normal View History

2016-02-22 06:05:44 +00:00
const cors = require('cors')
2016-04-12 11:25:38 +00:00
const express = require('express')
const createRequestHandler = require('npm-http-server').createRequestHandler
const onFinished = require('on-finished')
const redis = require('redis')
2016-02-22 02:52:19 +00:00
2016-03-06 05:59:53 +00:00
const registryURL = process.env.npm_package_config_registryURL
const bowerBundle = process.env.npm_package_config_bowerBundle
2016-03-18 19:45:49 +00:00
const redirectTTL = process.env.npm_package_config_redirectTTL
2016-03-26 07:09:07 +00:00
const autoIndex = process.env.npm_package_config_autoIndex
2016-02-22 06:05:44 +00:00
const port = process.env.PORT || process.env.npm_package_config_port
2016-04-12 11:25:38 +00:00
const requestLogging = (redisURL) => {
const redisClient = redis.createClient(redisURL)
return (req, res, next) => {
2016-04-12 11:25:38 +00:00
onFinished(res, () => {
if (res.statusCode === 200)
redisClient.incr(req.path)
2016-04-12 11:25:38 +00:00
})
next()
}
}
2016-04-12 11:25:38 +00:00
2016-02-22 06:05:44 +00:00
const app = express()
2016-02-22 02:52:19 +00:00
app.disable('x-powered-by')
app.use(cors())
app.use(express.static('public', { maxAge: 60000 }))
2016-04-12 11:25:38 +00:00
if (process.env.REDIS_URL)
app.use(requestLogging(process.env.REDIS_URL))
2016-04-12 11:25:38 +00:00
app.use(createRequestHandler({
registryURL: registryURL,
2016-03-18 19:45:49 +00:00
bowerBundle: bowerBundle,
2016-03-26 07:09:07 +00:00
redirectTTL: redirectTTL,
autoIndex: autoIndex
}))
2016-02-22 02:52:19 +00:00
app.listen(port, function () {
2016-03-07 03:29:49 +00:00
console.log('Server started on port %s, Ctrl+C to quit', port)
2016-02-22 02:52:19 +00:00
})