Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending Headers? #17

Open
djlerman opened this issue Aug 29, 2015 · 8 comments
Open

Sending Headers? #17

djlerman opened this issue Aug 29, 2015 · 8 comments

Comments

@djlerman
Copy link

Hello.

I've tried various ways to send a header and I get errors. What is the preferred way to send a header?

This is the error I get:
EXCEPTION: header is not defined

Here is what I'm trying to do.

<?
var http  = require('http');
var fs    = require('fs');

fs.readFile('/home/userName/var/lib/images/image.jpg', function(err, data) {
  if (err) throw err; // Fail if the file can't be read.
  header('Content-Type', 'image/jpeg');
  write(data);
});
?>
@jaszhix
Copy link

jaszhix commented Sep 1, 2015

I would try this instead:

response.headers = {'Content-Type': 'image/jpeg'};

@djlerman
Copy link
Author

djlerman commented Sep 4, 2015

I moved the response.headers outside of the function and added header for 'Content-Length'.

I got a little bit further. Any suggestions on how to get passed this error?

Now it is returning the error:

The image "https://www.iqaat.com/testScripts/image1.jss" cannot be displayed because it contains errors.
<?
  var fs      = require('fs');

  var stats = fs.statSync('/home/userName/var/lib/images/image.jpg');
  var fileSizeInBytes = stats["size"];

  response.headers = {'Content-Type': 'image/jpeg'};
  response.headers['Content-Length'] = fileSizeInBytes;
  response.sendHeaders();

  fs.readFile('/home/userName/var/lib/images/image.jpg', function(err, data) {
    if (err) throw err; // Fail if the file can't be read.

    response.write(data);
  });
?>

@jaszhix
Copy link

jaszhix commented Sep 4, 2015

Not sure what could be the issue, but you could try writing some vars to a debug log. Try this...

function log (data) {
  var timeNow = new Date(Date.now());
  var timeStamp = timeNow.toTimeString();
  var json = {
    time: timeStamp,
    data: data
  };
  var output = JSON.stringify(json);
  fs.appendFile('../api/debug.log', output + '\n', {flags : 'w'});
}

Then log(var).

@jaszhix
Copy link

jaszhix commented Sep 4, 2015

I tried loading that image, and it returned this.

^Q?t
95{[
xV-C
Oird
\F=I
<br/><div style="color:red"><b>EXCEPTION</b>: undefined is not a function<i><pre>TypeError: undefined is not a function
    at CgiHttpResponse.end (/home1/iqaatcom/public_html/cgi-bin/cgi-node.js:493:11)
    at /home1/iqaatcom/public_html/testScripts/image1.jss:15:14
    at fs.js:340:14
    at FSReqWrap.oncomplete (fs.js:101:15)</pre></i></div></br>

That gives you a line number. Also try moving everything but the headers to a separate file using include('file.js'). Put response.sendHeaders(); at the end of your JSS file.

@djlerman
Copy link
Author

djlerman commented Sep 4, 2015

How did you get all of the extra return text? That would be helpful with trying to get this to work.

@jaszhix
Copy link

jaszhix commented Sep 5, 2015

I used cURL, but can't reproduce the error. I suspect your headers are wrong. Try changing your content-type to text/html. Use the log function I pasted to debug the code. Good luck!

@djlerman
Copy link
Author

djlerman commented Sep 6, 2015

The only thing I can think of, is that CgiHttpRequest and CgiHttpResponse are not ready yet so something is not working on sending the data and ending the request. I'll have to use PHP until they are ready. :-(

Is there a time frame for the next release?

@jaszhix
Copy link

jaszhix commented Sep 6, 2015

No, the author isn't maintaining this repo. I have no part in cgi-node development. That you can see an error from cgi-node in curl would suggest its your code, not the cgi wrapper. Undefined means one of your variables are not returning the value they are supposed to. My guess is you used two fs calls on the same file and the encoding for the image is binary, something cgi-node can't handle by itself. You could try a helper such as node-image.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants