Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send response to client in nodejs

Unable to send response data back to client. Its throwing error saying response.write() is not a function:

var express = require('express');
var app = express();
var request = require('request');

var port = process.env.PORT || 5000;
app.set('port', (port));

app.use(express.static(__dirname + '/'));
app.get('/', function(request, response) {
  response.render('/');
});

app.listen(app.get('port'), function() {
  console.log('Node app is running on port', app.get('port'));
});

app.post('/login', verifyLogin);

function verifyLogin(req, res, next) {
    var loginParams = {
        'username': req.body.username,
        'password': req.body.password
    };

    request({
        url: 'http://localhost:8084/xxx/auth', //URL to hit
        qs: {username: req.body.username, password: req.body.password},
        method: 'POST',
        json: {
            "username": req.body.username, "password": req.body.password
        }
        }, function(error, response, body){
        if(error) {
            console.log(error);
        } else {
        response.write(body); // ERROR here response.write is not a function
        return response;
    }
});

I am getting the response data in command prompt console but how do I send response data back to client?

like image 701
kittu Avatar asked Nov 26 '16 05:11

kittu


People also ask

How do I send a response in NodeJS?

setHeader('Content-Type', 'text/html'); this line will set the format of response content o text/html. write() function method on response object can be used to send multiple lines of html code like below. res. write('<html>'); res.

Which express method sends a response to the client?

Response Render method This method renders a view and sends the rendered HTML string to the client.


1 Answers

res.send() is used to send response to the client.

function verifyLogin(req, res, next) {
    var loginParams = {
        'username': req.body.username,
        'password': req.body.password
    };

    request({
        url: 'http://localhost:8084/xxx/auth', //URL to hit
        qs: {username: req.body.username, password: req.body.password},
        method: 'POST',
        json: {
            "username": req.body.username, "password": req.body.password
        }
        }, function(error, response, body){
        if(error) {
            console.log(error);
        } else {
        //response.write(body); // ERROR here response.write is not a function

          res.send(body);// AND IT SHOULD BE USUALLY TRUE OR WITH THE OBJECT
//SO IT CAN ALSO BE 
             res.send(true);
//            return response;
        }
    });

For instance, It is written as

var express = require('express');
var app = express();

app.get('/', function(req, res){
  res.send('hello world');
});

app.listen(3000);

Here is the reference http://expressjs.com/en/api.html

like image 131
Tirthraj Rao Avatar answered Sep 20 '22 18:09

Tirthraj Rao