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 8973299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:24:52+00:00 2026-06-15T18:24:52+00:00

I am building an http server using node.js and am trying to send an

  • 0

I am building an http server using node.js and am trying to send an html page for a bad request. Here’s my code:

function respond(request, socket) {
    console.log("Request for " + request["url"] + ".");
    fs.readFile(request["url"], request["encoding"], function(error, file) {
        if(error) {
            console.log(error);
            writeHead(socket,request["version"],404,"text/html",null);
            socket.write(notFound(request["url"]));
            socket.end();
        } else {
            fs.stat(request["url"],function (err,stats) {
                writeHead(socket,request,200,stats)
                socket.write(file, request["encoding"]);
                socket.end();
            });
        }
    });
}

function notFound(url) {
return "<html> \
    <head> \
    <title>404 Not Found</title> \
    </head> \
    <body> \
    <h1>Not Found</h1> \
    <p>The requested URL " + url + " was not found on this server.</p> \
    <hr> \
    <address>EX4 Server</address> \
    </body> \
    </html>"
}

function writeHead(socket, request, code, stats) {
    socket.write(request["version"] + " " + code + " " + "\r\n");
    socket.write("Date: " + getDate() + "\r\n");
    if (stats != null) {
        socket.write("Content-Length: " + stats.size + "\r\n");
        socket.write("Last-Modified: " + stats.mtime + "\r\n");
    }
    socket.write("Server: EX4" + "\r\n");
    socket.write("Content-Type: " + request["contentType"] + "\r\n");
    socket.write("\r\n");
    return;
}

As you can see, if the file couldn’t be opened I call writeHead() which always ends with \r\n\r\n and then I send an html page.

However, what happens is that I just see the entire response (headers + body) on my browser (chrome if it matters).

  • 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-06-15T18:24:53+00:00Added an answer on June 15, 2026 at 6:24 pm

    I think you forgot a textual representation of your status code.

    HTTP/1.x 200 OK  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

using node.js, the net module for building a tcp server which can hande http
I am building a page using blocks of sections: http://jsfiddle.net/NrkTn/3/ You can see I
I'm building a chat application using OpenFire server and JSJaC client library. The page
server.cpp - http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/example/ssl/server.cpp I am trying to build this on my mac. Using the
I'm building a client that talks to the http server. Now my client needs
I'm building a Web app in codeigniter. Instead of the http server speaking to
I'm building up my site over at http://royronalds.com , and I'm trying to figure
The application I'm building right now has problems when using HTTP post. When timeout
I am building a PhoneGap/Cordova app and have a simple code that uses http://ricostacruz.com/jquery.transit/
The on/off switches shown on http://developer.android.com/design/building-blocks/switches.html#switches are awesome. I can't figure out how to

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.