Oscar Reyes
Oscar Reyes

Reputation: 4342

logging to all requests

i have a problem with the logging, but i dont have a clue how to stop it, i just want it to log for the view file and not for static files..

this is my code:

//logger.
app.use(function(request, response, next)
{
    if(app.get('env') == 'development')
    {
        console.log('%s %s', request.method, request.url);
        console.log('connection from: ' + request.ip);
    }

    var file = request.url.slice(1 + request.url.indexOf('/'));

    app.get(request.url, function(request, response)
    {
        response.render(file,
        {
            //Var to be named in the render : value;
            'Title': Title,
            'result': result
        });
    });

    next();
});


//Set directory for static files (css, js, img)
app.use(express.static(__dirname + '/public'));

when i get into the console to see the results. i get this

GET /css/bootstrap.css
connection from: 127.0.0.1
GET /css/cover.css
connection from: 127.0.0.1
GET /index
connection from: 127.0.0.1
GET /css/cover.css
connection from: 127.0.0.1
GET /css/bootstrap.css
connection from: 127.0.0.1

i just want it to log GET /index and not the rest.

Upvotes: 1

Views: 4318

Answers (1)

Ben
Ben

Reputation: 5074

if you're using express.js, just use logging middleware such as 'morgan' and let express handles the logging for you. For example the following code will print some logging automatically:

var express = require('express');
var morgan = require('morgan');  // Logging middleware
var app = express();

app.use(morgan('dev'));  // 'dev', 'short', 'tiny'. or no argument (default)
app.get('/', function(req, res){
    res.send('hello world');
});
app.listen(3000);

When you hit localhost:3000, you'll see the logging from express as (in color too):

GET / 200 3ms - 11b

Upvotes: 4

Related Questions