Log successful requests to redis instance if available
This commit is contained in:
parent
3180839192
commit
8e1b8edca7
|
@ -12,7 +12,8 @@
|
|||
"cors": "^2.7.1",
|
||||
"express": "^4.13.4",
|
||||
"npm-http-server": "^2.9.1",
|
||||
"on-finished": "^2.3.0"
|
||||
"on-finished": "^2.3.0",
|
||||
"redis": "^2.6.0-1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
|
14
server.js
14
server.js
|
@ -2,6 +2,7 @@ const cors = require('cors')
|
|||
const express = require('express')
|
||||
const createRequestHandler = require('npm-http-server').createRequestHandler
|
||||
const onFinished = require('on-finished')
|
||||
const redis = require('redis')
|
||||
|
||||
const registryURL = process.env.npm_package_config_registryURL
|
||||
const bowerBundle = process.env.npm_package_config_bowerBundle
|
||||
|
@ -9,15 +10,18 @@ const redirectTTL = process.env.npm_package_config_redirectTTL
|
|||
const autoIndex = process.env.npm_package_config_autoIndex
|
||||
const port = process.env.PORT || process.env.npm_package_config_port
|
||||
|
||||
const requestLogging = () =>
|
||||
(req, res, next) => {
|
||||
const requestLogging = (redisURL) => {
|
||||
const redisClient = redis.createClient(redisURL)
|
||||
|
||||
return (req, res, next) => {
|
||||
onFinished(res, () => {
|
||||
if (res.statusCode === 200)
|
||||
console.log(req.url)
|
||||
redisClient.incr(req.url)
|
||||
})
|
||||
|
||||
next()
|
||||
}
|
||||
}
|
||||
|
||||
const app = express()
|
||||
|
||||
|
@ -25,8 +29,8 @@ app.disable('x-powered-by')
|
|||
app.use(cors())
|
||||
app.use(express.static('public', { maxAge: 60000 }))
|
||||
|
||||
if (process.env.LOG_URLS)
|
||||
app.use(requestLogging())
|
||||
if (process.env.REDIS_URL)
|
||||
app.use(requestLogging(process.env.REDIS_URL))
|
||||
|
||||
app.use(createRequestHandler({
|
||||
registryURL: registryURL,
|
||||
|
|
Loading…
Reference in New Issue