Rockstar22
Rockstar22

Reputation: 31

How do I send data back to the browser from the server?

I have a form on a webpage that asks for the client's username, password, age, and gender. I submit it to the nodejs server (express framework) when the user clicks the submit button. I want to query it against a database to check if the username and password already exists and if it does, can I send a message back to the browser saying the password or username already exists and somehow catch that message with javascript and use it to modify the page (to display the message that the username or password already exists) without using a template engine like jade or ejs?

Upvotes: 2

Views: 1422

Answers (1)

Andzej Maciusovic
Andzej Maciusovic

Reputation: 4476

You can implement Rest post method to post your data like. Server code:

router.post('/create-user', function (req, res) {
  var user = req.body;
  usersService.createUser(user).then(function (status) {
    res.json(status);
  });
});

and in your client js code you need to have success method to check status. You can use jQuery post method

$.post( "api/create-user", formData , function( result) {
   $( ".result" ).html( result );
});

Upvotes: 1

Related Questions