I have a html page that triggers an ajax request:
$.ajax({
type: 'POST',
url: '/usernamecheck',
data: {"username":username},
success: function(taken){
if(taken === 0){
$('#error').text('The username' + username + ' is available!')
}else{
$('#error').text('The username' + username + ' is not available')
}
},
dataType: "json"
})
this is my node.js code:
exports.usernameCheck = function(req,res){
var db;
db = require('./../custom_modules/db.js');
var username = req.body.username;
db.users.find({username:username},function(err,users){
console.log(username)
if(users.length === 0){
//return 0
}else{
//return1
}
})
and I want to respond to this ajax with node.js request but am a little unsure of how to do so?
res.render()what is the equivelent for sending JSON?res.send()can take objects and will JSON.stringify them, too, now.