Skip to content

Instantly share code, notes, and snippets.

@timneutkens
Created January 3, 2017 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timneutkens/9d50642480339b1f8797fae3a257d01f to your computer and use it in GitHub Desktop.
Save timneutkens/9d50642480339b1f8797fae3a257d01f to your computer and use it in GitHub Desktop.
Use Morgan logger with Micro
// Based on https://www.npmjs.com/package/morgan#vanilla-http-server
const { send } = require('micro')
const morgan = require('morgan')
const logger = morgan('combined')
module.exports = async function (req, res) {
logger(req, res, function (err) {
if (err) {
return send(res, 500, 'Error')
}
send(res, 200, 'OK!')
})
})
// Using pify to turn logger into a Promise.
const { send } = require('micro')
const pify = require('pify')
const morgan = require('morgan')
const logger = pify(morgan('combined'))
module.exports = async function (req, res) {
await logger(req, res)
return 'OK!'
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment