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",
|
"cors": "^2.7.1",
|
||||||
"express": "^4.13.4",
|
"express": "^4.13.4",
|
||||||
"npm-http-server": "^2.9.1",
|
"npm-http-server": "^2.9.1",
|
||||||
"on-finished": "^2.3.0"
|
"on-finished": "^2.3.0",
|
||||||
|
"redis": "^2.6.0-1"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|
14
server.js
14
server.js
|
@ -2,6 +2,7 @@ const cors = require('cors')
|
||||||
const express = require('express')
|
const express = require('express')
|
||||||
const createRequestHandler = require('npm-http-server').createRequestHandler
|
const createRequestHandler = require('npm-http-server').createRequestHandler
|
||||||
const onFinished = require('on-finished')
|
const onFinished = require('on-finished')
|
||||||
|
const redis = require('redis')
|
||||||
|
|
||||||
const registryURL = process.env.npm_package_config_registryURL
|
const registryURL = process.env.npm_package_config_registryURL
|
||||||
const bowerBundle = process.env.npm_package_config_bowerBundle
|
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 autoIndex = process.env.npm_package_config_autoIndex
|
||||||
const port = process.env.PORT || process.env.npm_package_config_port
|
const port = process.env.PORT || process.env.npm_package_config_port
|
||||||
|
|
||||||
const requestLogging = () =>
|
const requestLogging = (redisURL) => {
|
||||||
(req, res, next) => {
|
const redisClient = redis.createClient(redisURL)
|
||||||
|
|
||||||
|
return (req, res, next) => {
|
||||||
onFinished(res, () => {
|
onFinished(res, () => {
|
||||||
if (res.statusCode === 200)
|
if (res.statusCode === 200)
|
||||||
console.log(req.url)
|
redisClient.incr(req.url)
|
||||||
})
|
})
|
||||||
|
|
||||||
next()
|
next()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
|
|
||||||
|
@ -25,8 +29,8 @@ app.disable('x-powered-by')
|
||||||
app.use(cors())
|
app.use(cors())
|
||||||
app.use(express.static('public', { maxAge: 60000 }))
|
app.use(express.static('public', { maxAge: 60000 }))
|
||||||
|
|
||||||
if (process.env.LOG_URLS)
|
if (process.env.REDIS_URL)
|
||||||
app.use(requestLogging())
|
app.use(requestLogging(process.env.REDIS_URL))
|
||||||
|
|
||||||
app.use(createRequestHandler({
|
app.use(createRequestHandler({
|
||||||
registryURL: registryURL,
|
registryURL: registryURL,
|
||||||
|
|
Loading…
Reference in New Issue