1

I'm using a node.js server and need to post data over an http*s* protocol.

POST is listed as a http protocol, so I'm wondering if I'm on the wrong track.

Anyway, here is the top of my code that handles the POST. I've not included the on('end') function for berevity, but it is there.

exports.login = function(req, res){
if(req.method == 'POST'){
    var datastring = '';
    req.on('data', function(data){
        datastring += data;
        console.log("getting data");
        if(datastring.length > 1e6){
            req.connection.destroy();
        }
    });

Through console logs I can determine that the request is being made, and that the request is recognised as POST, but that req.on('data')'s function is never called.

I looked into a CONNECT protocol, but this isn't supported by browser forms - please advise how to send data through CONNECT if you don't think I can send POST data over HTTPS

Thanks for your help

EDIT: here is form that sends request:

<form name="loginForm" action="login" method="POST" onsubmit="return checkForm()" class="separate-sections">

and here is node.js code that grabs post

var app= express();
...
app.post('/login', route_login.login);

To make a long story short, does POST work over HTTPS? It doesn't seem to be working here.

binderbound
  • 791
  • 1
  • 7
  • 27
  • 1
    HTTPS isn’t really that kind of protocol. TLS/SSL are at a different level than HTTP. You shouldn’t need to worry about either. Have you called `req.end();`? – Ry- Oct 30 '13 at 01:32
  • req.on('end') is there, I just haven't included it. It is never called, which is to be expected if he on('data') function is never called either – binderbound Oct 30 '13 at 01:38
  • No, not `req.on("end")`. When you’re sending POST data, you have to end the request. Call `req.end()`. – Ry- Oct 30 '13 at 01:39
  • I have nearly identical code that works fine – markasoftware Oct 30 '13 at 01:47
  • the only possibility I can think of is adding an `end` handler and . also having JUST this in the `data` handler: `datastring += data;` also, can you show us the code that SENDS the post request as well? – markasoftware Oct 30 '13 at 01:50
  • where exactly do I put req.end()? – binderbound Oct 30 '13 at 02:07
  • i don't think you need req.end(), they read your question incorrectly. They thought that you were trying to use node.js to CREATE a post request while it seems that you are actually trying to recieve one. – markasoftware Oct 30 '13 at 02:11
  • so you've put in console.log into here and you HAVE been able to see that the request came to the server and the server was able to recognize it as a POST request? If you've gotten that far, then HTTPS cannot be the problem, I think. – markasoftware Oct 30 '13 at 02:15
  • That is what I mean to do, yes. – binderbound Oct 30 '13 at 02:16
  • the request does come into the server and is handled fine, but seemingly no data is ever sent - it just waits for data from the post. – binderbound Oct 30 '13 at 02:23
  • Just realized you would need to copy all chunks and parse the form data before you can get anything. See here : http://stackoverflow.com/questions/4295782/how-do-you-extract-post-data-in-node-js – user568109 Oct 30 '13 at 04:22
  • I think I am doing that. req('on') data is adding the chunks to a datastring, and later on, when req('end') is called, I parse the data. The problem is that I'm not getting any chunks at all. – binderbound Oct 30 '13 at 04:32

1 Answers1

0

Found out someone on my team had added the express bodyparser, so naturally posts no longer work in the way I had before. The change just happened to coincide with when we switched to https which made it look as if this was the issue, when it was not.

binderbound
  • 791
  • 1
  • 7
  • 27