最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Send response to client in nodejs - Stack Overflow

programmeradmin3浏览0评论

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 mand prompt console but how do I send response data back to client?

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 mand prompt console but how do I send response data back to client?

Share Improve this question asked Nov 26, 2016 at 5:07 kittukittu 7,01821 gold badges103 silver badges199 bronze badges 1
  • use response.send() or res.send() function instead – Binit Ghetiya Commented Nov 26, 2016 at 5:21
Add a ment  | 

2 Answers 2

Reset to default 6

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./en/api.html

I think you're confusing your response.write with res.write?

You're using the response from the request callback, and not the res from the app.post callback

发布评论

评论列表(0)

  1. 暂无评论