Nodejs 服务器端与客户端之间的操纵

Nodejs 服务器端与客户端之间的操纵

这是一个小的顺序援用。经由过程客服端实行顺序。服务端接收顺序并打印文本

服务器端 Server.js

//nodejs Serverweb服务器
var qs = require('querystring');

require('http').createServer(function(req,res){
     var body ='';
    //猎取数值
    req.on('data',function(chunk){
        body += chunk;
    });
    req.on('end',function(){
        res.writeHead(200);
        res.end('Done');
        //打印输出文本!
        console.log('\n  获得名字 : \033[90m' + qs.parse(body).name + '\033[39m\n');
    });
}).listen(3000);

客户端 client.js

//客户端
var http =require('http'),
qs = require('querystring')

function send(thename){
    http.request({
        host:'127.0.0.1',
        port:3000,
        url:'/',
        method:'POST'
    },function(res){
    
    res.setEncoding('utf8');
    res.on('end',function(){
        console.log('\n  获得名字 : \033[90m request  \033[39m\n');
        process.stdout.write('\n 你的名字: ');
    });

    }).end(qs.stringify({name:thename})); //数据是经由过程end要领发送的
}

//stdout 是读,stdin 是写
process.stdout.write('\n 你的名字: ');
process.stdin.resume();
process.stdin.setEncoding('utf8');

process.stdin.on('data',function(name){
    send(name.replace('\n',''));
});

效果图:
《Nodejs 服务器端与客户端之间的操纵》

    原文作者:Lucky_huang
    原文地址: https://segmentfault.com/a/1190000004403503
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞