在浏览器中没有任何响应

在浏览器中没有任何响应

问题描述:

这是我的客户代码.

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");

这是我的客户代码,向上述服务器发出请求.问题是上述服务器给出的响应(即"User1,User2")正在控制台中显示,而不在浏览器中显示.

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");

替换

res.write(body);

使用

res.send(body);

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

那不是该页面的唯一问题),但这会让您入门.

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