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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:44:54+00:00 2026-05-22T22:44:54+00:00

Is that code valid HTTP/1.1? var fs = require(‘fs’) var http = require(‘http’) var

  • 0

Is that code valid HTTP/1.1?

var fs = require('fs')
var http = require('http')

var buf=function(res,fd,i,s,buffer){
 if(i+buffer.length<s){
  fs.read(fd,buffer,0,buffer.length,i,function(e,l,b){
   res.write(b.slice(0,l))
   //console.log(b.toString('utf8',0,l))
   i=i+buffer.length
   buf(res,fd,i,s,buffer)
  })
 }
 else{
  fs.read(fd,buffer,0,buffer.length,i,function(e,l,b){
   res.end(b.slice(0,l))
   fs.close(fd)
  })
 }
}

var app = function(req,res){
 var head={'Content-Type':'text/html; charset=UTF-8'}
 switch(req.url.slice(-3)){
  case '.js':head={'Content-Type':'text/javascript'};break;
  case 'css':head={'Content-Type':'text/css'};break;
  case 'png':head={'Content-Type':'image/png'};break;
  case 'ico':head={'Content-Type':'image/x-icon'};break;
  case 'ogg':head={'Content-Type':'audio/ogg'};break;
  case 'ebm':head={'Content-Type':'video/webm'};break;
 }
 head['Transfer-Encoding']='chunked'
 res.writeHead(200,head)
 fs.open('.'+req.url,'r',function(err,fd){
  fs.fstat(fd,function(err, stats){
   console.log('.'+req.url+' '+stats.size+' '+head['Content-Type']+' '+head['Transfer-Encoding'])
   var buffer = new Buffer(100)
   buf(res,fd,0,stats.size,buffer)
  })
 })
}

http.createServer(app).listen(8000,"127.0.0.1")
console.log('GET http://127.0.0.1:8000/appwsgi/www/index.htm')

I think I am violating HTTP/1.1 here? Text files do seem to work fine, but that could be coincidental. Is my header “200 OK” or need it to be “100”? Is one header sufficient?

  • 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-22T22:44:55+00:00Added an answer on May 22, 2026 at 10:44 pm

    If you’re doing chunked transfer encoding, you actually need to set that header:

    Transfer-Encoding: chunked

    You can see from the headers returned by google, which does chunked transfers for the homepage and most likely other pages:

    HTTP/1.1 200 OK
    Date: Sat, 04 Jun 2011 00:04:08 GMT
    Expires: -1
    Cache-Control: private, max-age=0
    Content-Type: text/html; charset=ISO-8859-1
    Set-Cookie: PREF=ID=f9c65f4927515ce7:FF=0:TM=1307145848:LM=1307145848:S=fB58RFtpI5YeXdU9; expires=Mon, 03-Jun-2013 00:04:08 GMT; path=/; domain=.google.com
    Set-Cookie: NID=47=UiPfl5ew2vCEte9JyBRkrFk4EhRQqy4dRuzG5Y-xeE---Q8AVvPDQq46GYbCy9VnOA8n7vxR8ETEAxKCh-b58r7elfURfiskmrOCgU706msiUx8L9qBpw-3OTPsY-6tl; expires=Sun, 04-Dec-2011 00:04:08 GMT; path=/; domain=.google.com; HttpOnly
    Server: gws
    X-XSS-Protection: 1; mode=block
    Transfer-Encoding: chunked
    

    EDIT Yikes, that read is way too complicated:

    var app = function(req,res){
     var head={'Content-Type':'text/html'}
     switch(req.url.slice(-3)){
      case '.js':head={'Content-Type':'text/javascript'};break;
      case 'css':head={'Content-Type':'text/css'};break;
      case 'png':head={'Content-Type':'image/png'};break;
      case 'ico':head={'Content-Type':'image/x-icon'};break;
      case 'ogg':head={'Content-Type':'audio/ogg'};break;
      case 'ebm':head={'Content-Type':'video/webm'};break;
     }
     res.writeHead(200,head)
     var file_stream = fs.createReadStream('.'+req.url);
     file_stream.on("error", function(exception) {
       console.error("Error reading file: ", exception);
     });
     file_stream.on("data", function(data) {
       res.write(data);
     });
     file_stream.on("close", function() {
       res.end();
     });
    }
    

    There you go, a nice streamed buffer for you to write with. Here’s a blog post I wrote on different ways to read in files. I recommend looking that over so you can see how to best work with files in node’s asynchronous environment.

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

Sidebar

Related Questions

I'm about to write a Wrapper for a COM interop IStream so that code
The code that I want to write is like this: void MethodOnThreadA() { for
Sample code that shows how to create threads using MFC declares the thread function
I have a site that has an IE8-only problem: The code is: var w
I'm attempting to write a performance testing function that can take any function, run
I have this JS code: var recipesPost = function(name, ingredients, steps) { $.post('/api/recipes', {
Is the following code valid? function test() { return /\//.exec(\/); } alert(test()); It seems
I have heard people state that Code Generators and T4 templates should not be
I don't understand why authors said that Code Listing 9.1 from Programming in Scala
What Visual Studio settings and .emacs macros improve the likelihood that code written on

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.