本文介绍了在浏览器中没有任何响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的客户代码.

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.

这篇关于在浏览器中没有任何响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 13:57