Send post request from client to node.js
- by Husar
In order to learn node.js I have built a very simple guestbook app. There is basically just a comment form and a list of previous comments. Currently the app is client side only and the items are stored within local storage.
What I want to do is send the items to node where I will save them using Mongo DB.
The problem is I have not yet found a way to establish a connection to send data back and forth the client and node.js using POST requests.
What I do now is add listeners to the request and wait for data I send:
request.addListener('data', function(chunk) {
console.log("Received POST data chunk '"+ chunk + "'.");
});
On the client side I send the data using a simple AJAX request:
$.ajax({
url: '/',
type: 'post',
dataType: 'json',
data: 'test'
})
This does not work at all in them moment. It could be that I don't know what url to place in the AJAX request 'url' parameter. Or the whole thing might just be the build using the wrong approach.
I have also tried implementing the method described here, but also with no success.
It would really help if anyone can share some tips on how to make this work (sending POST request from the client side to node and back) or share any good tutorials. thanks.