Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6079209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:55:15+00:00 2026-05-23T10:55:15+00:00

I’m trying to build a Node.js server with console logging similar to that of

  • 0

I’m trying to build a Node.js server with console logging similar to that of Django’s development server. E.g.

[27/Jun/2011 15:26:50] "GET /?test=5 HTTP/1.1" 200 545

The following server.js (based on the Node Beginner Book tutorial) gets me the time and request information:

var http = require("http");
var url = require ("url");
var port = 1234;

function start(route, handle) {
    function onRequest(request, response) {
        var pathname = url.parse(request.url).pathname;
        var query = url.parse(request.url).query;
        route(handle, pathname, query, response);
        logRequest(request);
    }
    http.createServer(onRequest).listen(port);
    console.log("\nServer running at http://192.168.1.5:" + port + "/");
    console.log("Press CONTROL-C to quit.\n");
}

function logRequest(request) {
    var pathname = url.parse(request.url).pathname;
    var query = url.parse(request.url).query;
    if (query == undefined) {
        query = "";
    }
    var currentDate = new Date();
    var day = currentDate.getDate();
    var month = currentDate.getMonth() + 1;
    var year = currentDate.getFullYear();
    var hours = currentDate.getHours();
    var minutes = currentDate.getMinutes();
    var seconds = currentDate.getSeconds();
    if (minutes < 10) {
        minutes = "0" + minutes;
    }
    if (seconds < 10) {
        seconds = "0" + seconds;
    }
    console.log("[" + year + "/" + month + "/" + day + 
                " " + hours + ":" + minutes + ":" + seconds + '] "' +
                request.method + " " + pathname + query +
                " HTTP/" + request.httpVersion + '"');
}    

exports.start = start;

Question: how would I update this code to get the response.statusCode and whatever the “545” number is into the log output?

When I tried adding the response object to the logRequest function, the response statusCode was always “200”, even when I knew (via debug logging) that my router.js was generating 404 errors.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:55:15+00:00Added an answer on May 23, 2026 at 10:55 am

    If you are using the route method found in the tutorial you linked, then the reason why using the response.statusCode doesn’t work is because they set the 404 status code using this line

    response.writeHead(404, {"Content-Type": "text/html"});
    

    If you take a look at the documentation right there Node.JS Docs – writeHead it states that .writeHead will not set the .statusCode value. If you want to use the .statusCode value, you should change the line in the route method to read like this :

    response.statusCode = 404;
    response.setHeader("Content-Type", "text/html");
    

    Directly assigning a value to statusCode and using “implicit” header declarations (setHeader instead of writeHead) will produce the same result for the user, but on your side you will have access to the statusCode later on in your log method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.