Created
January 3, 2017 19:56
-
-
Save timneutkens/9d50642480339b1f8797fae3a257d01f to your computer and use it in GitHub Desktop.
Use Morgan logger with Micro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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