morgan: correct morgan function signature

This commit is contained in:
Paolo Scanferla
2017-09-09 10:27:56 +02:00
parent 10fc78ad77
commit 96610b3fa3
2 changed files with 34 additions and 13 deletions
+5 -5
View File
@@ -24,7 +24,7 @@ declare namespace morgan {
*/
export interface Morgan {
/***
* Create a new morgan logger middleware function using the given format and options. The format argument may be a string of a predefined name (see below for the names),
* Create a new morgan logger middleware function using the given format and options. The format argument may be a string of a predefined name (see below for the names),
* or a string of a format string containing defined tokens.
* @param format
* @param options
@@ -69,7 +69,7 @@ declare namespace morgan {
(format: 'tiny', options?: Options): express.RequestHandler;
/***
* Create a new morgan logger middleware function using the given format and options. The format argument may be a
* Create a new morgan logger middleware function using the given format and options. The format argument may be a
* custom format function which adheres to the signature.
* @param format
* @param options
@@ -152,7 +152,7 @@ declare namespace morgan {
}
/***
* Create a new morgan logger middleware function using the given format and options. The format argument may be a string of a predefined name (see below for the names),
* Create a new morgan logger middleware function using the given format and options. The format argument may be a string of a predefined name (see below for the names),
* or a string of a format string containing defined tokens.
* @param format
* @param options
@@ -200,11 +200,11 @@ declare function morgan(format: 'short', options?: morgan.Options): express.Requ
declare function morgan(format: 'tiny', options?: morgan.Options): express.RequestHandler;
/***
* Create a new morgan logger middleware function using the given format and options. The format argument may be a
* Create a new morgan logger middleware function using the given format and options. The format argument may be a
* custom format function which adheres to the signature.
* @param format
* @param options
*/
declare function morgan(custom: (req: express.Request, res: express.Response) => string): express.RequestHandler
declare function morgan(format: morgan.FormatFn, options?: morgan.Options): express.RequestHandler
export = morgan;
+29 -8
View File
@@ -15,16 +15,37 @@ morgan('tiny')
morgan(':remote-addr :method :url')
morgan('combined', {
buffer: true,
immediate: true,
skip: function (req, res) { return res.statusCode < 400 },
stream: {
write: (str: string) => {
console.log(str);
}
buffer: true,
immediate: true,
skip: function (req, res) { return res.statusCode < 400 },
stream: {
write: (str: string) => {
console.log(str);
}
}
});
// a custom format function
morgan((tokens, req, res) => {
return [
tokens.method(req, res),
tokens.url(req, res),
tokens.status(req, res),
tokens.res(req, res, 'content-length'), '-',
tokens['response-time'](req, res), 'ms'
].join(' ');
});
// a custom format function with options
morgan((_tokens, _req, _res) => "", {
buffer: true,
immediate: true,
skip: function (req, res) { return res.statusCode < 400 },
stream: {
write: (str: string) => {
console.log(str);
}
}
});
// test interface definition for morgan
@@ -46,7 +67,7 @@ interface IExtendedFormatFn extends morgan.FormatFn {
var developmentExtendedFormatLine : IExtendedFormatFn = function(tokens, req, res):string {
// get the status code if response written
var status = res.statusCode
? res.statusCode