最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Morgan Logger with Express.js only logging failed requests - Stack Overflow

programmeradmin0浏览0评论

I'm using Morgan with Express.js and I think I have a problem with my middleware because Morgan is only logging requests returning error 302 (redirected), which is occurring when my middleware catches an error and redirects to its error page.

I'm fairly new to Express.js an all the middleware jazz so I would greatly appreciate any help. Thanks!

var http = require('http')
    , express = require('express')
    , app = express()
    , logger = require('morgan');

app.use(express.static(__dirname + '/public'));
app.use(logger());

var server = http.createServer(app);
server.listen(8888);

// Handle 404
app.use(function(req, res) {
    res.status(404).redirect('/error/404.html');
});

// Handle 500
app.use(function(error, req, res, next) {
    res.status(500).redirect('/error/500.html');
});

I'm using Morgan with Express.js and I think I have a problem with my middleware because Morgan is only logging requests returning error 302 (redirected), which is occurring when my middleware catches an error and redirects to its error page.

I'm fairly new to Express.js an all the middleware jazz so I would greatly appreciate any help. Thanks!

var http = require('http')
    , express = require('express')
    , app = express()
    , logger = require('morgan');

app.use(express.static(__dirname + '/public'));
app.use(logger());

var server = http.createServer(app);
server.listen(8888);

// Handle 404
app.use(function(req, res) {
    res.status(404).redirect('/error/404.html');
});

// Handle 500
app.use(function(error, req, res, next) {
    res.status(500).redirect('/error/500.html');
});
Share Improve this question edited Jun 15, 2014 at 20:23 jczimm asked Jun 15, 2014 at 19:57 jczimmjczimm 3633 silver badges11 bronze badges 2
  • Why are you redirecting instead of rendering/sending the relevant html error file? Something like res.status(500).render('505_template'); or such? – Matthew Bakaitis Commented Jun 15, 2014 at 21:20
  • I've tried that. :/ Do you know what my problem is? The argument for app.render() is a path to a .html file, right? @MattBakaitis – jczimm Commented Jun 15, 2014 at 23:17
Add a ment  | 

1 Answer 1

Reset to default 5

In terms of 'getting it to work', the code below will do this for 404's.

I didn't answer anything regarding other response codes, leaving that to you. :)

Getting it to work

A summary of what I changed:

  1. I removed the dependency on 'http' and the var server = http.createServer(app); because those aren't really needed
  2. I moved app.listen to the end...so it listens after all express configuration is plete.
  3. I used the .sendfile() method instead of .redirect() since we're just sending status stuff around

This worked on my system. If you have questions about the code or issues with it leave a ment and I will help as I am able...but I can't promise miracles in ments. :)

var express = require('express');
var app = express();
var logger = require('morgan');

// edited, moving `app.use(logger());` first so it can log everything...    
app.use(logger());
app.use(express.static(__dirname + '/public'));


// Handle 404
app.use(function(req, res, next) {
    res.status(404).sendfile('./error/404.html');
});

app.listen(8888);

A quick aside:

My guess is that some of the issues in your code are based upon the fact that many express tutorials/guides were written for v.3.x but the changes in v.4.x are substantial enough that it's not super-easy to figure out what might still work and what doesn't.

For example, you'd only be using morgan with an express v.4.x app but several things in your code look like they came from a v.3.x app/example/tutorial. When I started, the same stuff was happening as express moved from v.2.x to v.3.x...so yeah. I feel your pain.

发布评论

评论列表(0)

  1. 暂无评论