I am comfortable in PHP but I needed to learn node.js to make my chat application more efficient. I am beginner in node.js and I am currently having the following problem.
What I want to do is to send request to node.js from ajax using jquery and get a response
I was able to do this easy with php but node.js is giving me error. Here is my code so far...
part of HTML
$(document).on("click", "#nodesess", function () {
$.ajax({
type: "POST",
url: "http://localhost:3000/",
success: function(data) {
alert('Success!')
},
error: function() {
alert('error')
}
});
});
on the server side
var express = require ('express');
var app= express();
app.get ('/', function(req,res){
res.send('Hello Express');
console.log("got get");
});
app.post ('/', function(req,res){
res.send('Hello Express post');
console.log("got post");
});
var server = app.listen(3000, function(){
console.log('listening on port 3000');
});
I have both post and get because I was testing for both. Furthermore, on the cmd i was able to see "got post" or "got get", I was just not able to return the text back to the ajax. Do I need to add Content-Type or I am I doing the whole thing wrong? In php, I would have done POST to a php file and in there I just need to echo. What ever I echoed will be sent back to the ajax....I was thinking res.send will be the same as echo....Help please
I think its important that i mention, the html file and the node.js file are not in the same directory. The Html is actually in the xampp folder and the node is in another directory...Is it even possible to do that?