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

  • Home
  • SEARCH
  • 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 6917235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:42:16+00:00 2026-05-27T09:42:16+00:00

I need to implement a HTTP server in node.js without using http module. How

  • 0

I need to implement a HTTP server in node.js without using http module. How fun!
I’m having trouble with sending the response socket.

I’m trying to fetch a file and so my code looks as follows:

fileStream = fs.createReadStream('example.jpg');
fileStream.on("end", function (close) {    
    var str = "HTTP/1.1 200 OK\r\Content-Type: image/jpeg\r\n" //and some more headers.
    socket.write(str);
    socket.pipe(fileStream);
    socket.end("\r\n");
});

What am I missing?

I am of course using net module to get the socket and fs as well.

  • 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-27T09:42:16+00:00Added an answer on May 27, 2026 at 9:42 am

    There are two main issues with the code you have.

    • Responses as formatted as headers followed by two sets of “\r\n”, and then the content. The “\r\n” in your end() call should be in str. You also are missing an ‘n’ from the first “\r\n”.
    • You are trying to pipe the contents of the readstream, which is great, but you are doing all of this inside of the steam’s end(), so the pipe as nothing left to send because the data was all already emitted.

    Try something like this instead. Create the read stream, send the response and pipe then rest.

    var str = "HTTP/1.1 200 OK\r\nContent-Type: image/jpeg\r\n\r\n";
    socket.write(str);
    var fileStream = fs.createReadStream('example.jpg');
    fileStream.pipe(socket);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to implement simple secured client server communiction using WCF. when im launching
I'm trying to implement Sun's example Socket program, i.e. the KnockKnock server and client
I need to implement the classic Factory Method pattern in ASP.NET to create server
I am in need of a scalable and performant HTTP application/server that will be
Trying to implement the excellent jQuery bidirectional infite scroll as explained here: http://www.bennadel.com/blog/1803-Creating-A-Bidirectional-Infinite-Scroll-Page-With-jQuery-And-ColdFusion.htm For
Details: I'm basically trying to implement the functionality of the example here ( http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/editondblclick/defaultvb.aspx
I'm using WinXP on clients and Win2003 on a server. I need do atomic
I want to implement HTTP basic authentication on my staging server, but only for
I need to create multi node web server that will be allow to control
I am trying to implement a HTTP based chat here. All the chat messages

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.