I am new to Express so I'd like to clear my code. This is Express with Redis support. I am getting someId
and then I am searching it on Redis or using scraper which accepts this ID. I am then using just prepared template jade and rendering it.
exports.run = (req, res) ->
return res.status(400).json(error: 'no someId') unless req.query.someId?
orderId = req.query.someId
redis.get someId, (err, response) ->
if err?
log.error config.redisErrorMessage if check err.message
log.warn err, 'Error during redis.get'
if response?
res.setHeader('content-type', 'text/html')
res.send response
else
scrapeData someId, res, (err, locals) ->
if err?
log.warn {err, orderId}, 'order not found'
return res.status(404).json(error: err.message)
if locals.redirect?
return res.redirect 301, locals.redirectUrl
res.app.render 'index', locals, (err, html) ->
if err?
log.warn err, 'render error'
return res.status(404).json(error: err.message)
else
# render without waiting setex
res.setHeader('content-type', 'text/html')
res.send html
# caching data after final render
redis.setex orderId, config.REDIS_CACHE_INTERVAL, html, (err) ->
if err?
if check err.message
log.error err, config.redisErrorMessage
else
log.error err, 'Error during redis.setex'