From d5631f54a30c5566bf991b662c790fbdf3957826 Mon Sep 17 00:00:00 2001 From: fenwick67 Date: Fri, 2 Aug 2019 13:24:31 -0400 Subject: [PATCH] improve logger --- index.js | 19 +++++-------------- lib/errorPage.js | 15 +++++++++------ npm-shrinkwrap.json | 32 ++++++++++++++++++++++++++++++++ package.json | 1 + 4 files changed, 47 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index bc9c6ef..290513c 100644 --- a/index.js +++ b/index.js @@ -7,20 +7,11 @@ var serveStatic = require('serve-static'); var request = require('request'); var cors = require('cors'); var errorPage = require('./lib/errorPage'); -var log = console.log; +var morgan = require('morgan'); var app = Express(); -function logMiddleware(req,res,next){ - log(req.method.toUpperCase() +' '+ req.url); - log( '\t'+ new Date().toISOString() ); - if(req.headers && req.headers.referer){ - log('\tReferer: '+req.headers.referer); - } - return next(null); -} - -app.use(logMiddleware); +var logger = morgan('tiny') app.use( serveStatic('static',{ @@ -64,7 +55,7 @@ app.get('/api/feed',cors(),function(req,res){ app.options('/apiv2/feed',cors()); // http://localhost:8000/apiv2/feed?userurl=https%3A%2F%2Foctodon.social%2Fusers%2Ffenwick67 -app.get('/apiv2/feed',cors(),function(req,res){ +app.get('/apiv2/feed',cors(),logger,function(req,res){ // get feed url var userUrl = req.query.userurl; @@ -118,12 +109,12 @@ app.get('/apiv2/feed',cors(),function(req,res){ res.send(data); }).catch((er)=>{ res.status(500); - res.send(errorPage(500)); + res.send(errorPage(500,null,{theme:opts.theme,size:opts.size})); // TODO log the error console.error(er,er.stack); }) }) app.listen(process.env.PORT || 8000,function(){ - log('listening on '+(process.env.PORT || 8000)); + console.log('Server started, listening on '+(process.env.PORT || 8000)); }); diff --git a/lib/errorPage.js b/lib/errorPage.js index 3e6d0cb..6f257c3 100644 --- a/lib/errorPage.js +++ b/lib/errorPage.js @@ -2,27 +2,30 @@ var ejs = require('ejs'); var fs = require('fs'); var template = ejs.compile(fs.readFileSync('./lib/template.ejs', 'utf8')); - -module.exports = function(code,message){ - +module.exports = function(code,message,displayOptions){ var msg; + var displayOptions = displayOptions || {}; if (code == 500 && !message){ - msg = '

Sorry, we are having trouble fetching posts for this user. Please try again later.


If the issue persists, please open an issue on GitHub or message fenwick67@octodon.social

' + msg = '

Sorry, we are having trouble fetching posts for this user. Please try again later.


If the issue persists, please open an issue on GitHub, or message fenwick67@octodon.social

' }else{ - msg = message; + msg = message||''; } var options = { opts:{ - header:true + header:true, + theme:displayOptions.theme||null, + size:displayOptions.size||null }, meta:{ title:code.toString(), description:msg, link:'#' + // avatar:'', + // headerImage:'' }, items:[], nextPageLink:null, diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 014b4a7..2a5f2c4 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -138,6 +138,21 @@ "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", "dev": true }, + "basic-auth": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz", + "integrity": "sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg==", + "requires": { + "safe-buffer": "5.1.2" + }, + "dependencies": { + "safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + } + } + }, "bcrypt-pbkdf": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz", @@ -997,6 +1012,18 @@ } } }, + "morgan": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/morgan/-/morgan-1.9.1.tgz", + "integrity": "sha512-HQStPIV4y3afTiCYVxirakhlCfGkI161c76kKFca7Fk1JusM//Qeo1ej2XaMniiNeaZklMVrh3vTtIzpzwbpmA==", + "requires": { + "basic-auth": "~2.0.0", + "debug": "2.6.9", + "depd": "~1.1.2", + "on-finished": "~2.3.0", + "on-headers": "~1.0.1" + } + }, "mri": { "version": "1.1.4", "resolved": "https://registry.npmjs.org/mri/-/mri-1.1.4.tgz", @@ -1131,6 +1158,11 @@ "ee-first": "1.1.1" } }, + "on-headers": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.2.tgz", + "integrity": "sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA==" + }, "once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", diff --git a/package.json b/package.json index b76b709..a658308 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "ejs": "^2.5.8", "express": "^4.16.4", "feedparser": "^2.2.9", + "morgan": "^1.9.1", "request": "^2.88.0", "request-promise-cache": "^2.0.1", "request-promise-native": "^1.0.7",