admin管理员组文章数量:1415460
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
1 Answer
Reset to default 5In 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:
- I removed the dependency on
'http'
and thevar server = http.createServer(app);
because those aren't really needed - I moved
app.listen
to the end...so it listens after all express configuration is plete. - 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.
本文标签: javascriptMorgan Logger with Expressjs only logging failed requestsStack Overflow
版权声明:本文标题:javascript - Morgan Logger with Express.js only logging failed requests - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745165821a2645671.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论