Mukul Chakravarty
Mukul Chakravarty

Reputation: 111

Not getting any response in the browser

This is my client code.

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

app.use(function(req,res,next){
    console.log(req.url,req.method);

    next();
});
app.get('/user/data',function(request,response)
{
    response.send("User1,User2");
    response.end();
});

var server=app.listen(7000);
console.log("started");

This is my client code that makes a request to the above server. The issue is that the response i.e. "User1,User2" given by the above server is getting displayed in the console but not on the browser.

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

var body="";
app.get('/data',function(req,res){
    var options=
    {
        'host':'localhost',
        'path':'/user/data',
        'port':'7000'
    }
    var call=function(response)
    {
        response.on('data',function(chunk)
        {
            body+=chunk;
        });
        response.on('end',function()
        {
            console.log(body);
            res.write(body);
        });

    }
    http.request(options,call).end();
});

app.listen(9000);
console.log("started");

Upvotes: 0

Views: 1252

Answers (2)

ulikus
ulikus

Reputation: 306

Replace

res.write(body);

With

res.send(body);

Or

res.write(body);
res.end();

Thats not the only issue with that page ) but that's will get you started.

Upvotes: 2

mattsch
mattsch

Reputation: 1484

Could you use request for this?

If you didn't need the console.log in your "client" code, you could have:

app.get('/data', function (req, res) {
  var options = {
    'host':'localhost',
    'path':'/user/data',
    'port':'7000'
  }

  request(options).pipe(res);
});

Upvotes: 0

Related Questions