0

I am using node.js. It's a fairly simple file arrangement on a local server.

<h3>To Do!</h3>
<ol class="pending>
<li><a class="pending_task" href="localhost://nextstep.html">Task Three</a></li>
<li><a class="pending_task" href="localhost://nextstep.html">Task Five</a></li>
<li><a class="pending_task" href="localhost://nextstep.html">Task Six</a></li>
</ol>

and

<h3>Completed!</h3>
<ol class="completed">
<li><a class="completed_task" href="localhost://nextstep.html">Task One</a></li>
<li><a class="completed_task" href="localhost://nextstep.html">Task Two</a></li>
<li><a class="completed_task" href="localhost://nextstep.html">Task Four</a></li>
</ol>

Are lists on the same page. I finally have the ajax call working as desired, and now I am in the newest glitch.

The $.ajax redirects to the proper url and the connect is made, but there is no data when I do a console.log(req.data)

The AJAX call:

var newData = "<ol class=\"pending\">" + $('ol.pending').html() + 
            "</ol>" + "<ol class=\"completed\">" + $('ol.completed').html() + "<\ol>";

        $.ajax({
            method:'POST',
            data: newData,
            url: ajax_url,
            success: function(result, status, jqxhr){
                console.log("Response: " + jqxhr.responseText);
                console.log("POST status: " + status);
            },
            dataType: 'html',
        });

The app.js

http.createServer(function(req, res){
  if ((req.method === 'POST') && (req.url === '/result')){
        console.log("Request Data: " + req.data);
        res.statusCode = 200;
        res.end("Message Received!");
  }
 }).listen(port, serverURL);

My intended result is to rewrite the originating file with the new arrangement of completed/pending, after writing the old arrangement to a new file name (TODO_date_time.html), which I can't do yet because the generated data, passed to the AJAX call is not being transmitted. Or I am not accessing it preoperly.

What I get though is:

[nodemon] starting `node app.js`
Starting Web Server at local:4000
Request Data: undefined

The console.log in the browser is all good:

Browser console.log

Not sure what's wrong with my work at this point.

Ken Ingram
  • 1,538
  • 5
  • 27
  • 52

2 Answers2

0

If you only need functionality, the suggested duplicate question above has a detailed explanation of simplifying a http server with Express.

Otherwise, the problem is that the data is not available yet because it comes in as chunks. To remedy this, event listeners are necessary inside where you detect that it's a POST request to the /result endpoint.

http.createServer(function(req, res) {
    if ((req.method === 'POST') && (req.url === '/result')) {
        console.log("POST request to /result received");

        req.on('data', function(chunk) {
            console.log('Received data: ', chunk.toString());
        });

        req.on('end', function() {
            res.statusCode = 200;
            res.end("Message Received!");
        });

    }
}).listen(port, serverURL);

Also, here's a link to a JSFiddle

Patrick Pei
  • 437
  • 4
  • 9
0

I used this

var body = ''     
request.on('data', function (data) {
  body += data;
});

from Casey Chu's answer in the question that I am possibly duplicating.

Ken Ingram
  • 1,538
  • 5
  • 27
  • 52