node代码片段

/**
 * Created by Administrator on 2016/8/22 0022.
 * chat
 */
var net=require('net');
var chatServer=net.createServer();
var clientList=[];
chatServer.on("connection",function(client){
    client.name=client.remoteAddress+":"+client.remotePort;
    console.log(client.name);//::ffff:192.168.1.100:53095 ->ipv6地址:ip地址:端口地址
    client.write('hello world');
    client.write('server build...!

');
    //client.end();//关闭客户端连接
    clientList.push(client);//把所有的客户端连接放进数组
    client.on("data",function(data){//监听客户端的data
        for(var i=0;i<clientList.length;i++){
            if(client!=clientList[i]){//当前的客户端不等于所有的客户端的当前客户端连接,就是排除当前客户端
                clientList[i].write(client.name+"say:"+data);//在当前客户端外的所有客户端打印当前客户端输入的信息
            }
        }
    });
    client.on('end',function(){
        clientLists.splice(clientList.indexOf(client),1);//断开客户端连接就把该客户端从客户端连接数组里删除
    });
    client.on('error',function(e){
       console.log(e)
    })

});
chatServer.listen(3000);

  

/**
 * Created by Administrator on 2016/8/22 0022.
 * pathParse
 */
var http=require('http');
var url=require('url');
var fs=require('fs');
var util=require('util');//外围函数模块
FILE='I:/work/wos/static';//定义目录
var httpServer=http.createServer(function(request,response){
    //response.writeHead(200,{'Content-Type':'text/plain'}); //mime
    response.writeHead(200,{'Content-Type':'text/html'});
    //response.write(util.inspect(url.parse(request.url)));//用url模块解析request.url,并用util.inspect字符串化解析的对象
    urlPath=FILE+url.parse(request.url).pathname;
    if(url.parse(request.url).pathname!='/favicon.ico'){ //排除favicon.ico的请求
        var resData=fs.readFileSync(urlPath,'utf8');
        response.write(resData);
    }
    response.end('end!');
});
httpServer.listen(8888);
console.log('server running at 8888');//日志

  

/**
 * Created by Administrator on 2016/8/23 0023.
 * postFormData
 */

var http=require('http'),
    url=require('url'),
    fs=require('fs'),
    queryString=require('querystring');
    //第三方Post数据解析模块 https://github.com/felixge/node-formidable
    formidable=require('formidable'); //formidable 翻译:可怕的,厉害的

http.createServer(function(req,res){
     var reqPathName=url.parse(req.url).pathname
     if(reqPathName=='/index'){
         var resDate=fs.readFile('static/index.html','utf-8',function(err,data){
             if(err){
                 console.log('服务器错误,错误代码:007');
             }else{
                 res.writeHead(200,{'Content-Type':'text/html'});
                 res.write(data);
                 res.end();
             }

         })
     }else if(reqPathName=='/post'){
       var postDate='';
       req.on('data',function(data){
           postDate+=data;
       });
       req.on('end',function(){
         var parsePostDate=queryString.parse(postDate);
           res.writeHead(200,{"content-type":'text/html'});
           res.write('<head><meta charset="utf-8"/></head>');
           res.write('提交的姓名是:'+parsePostDate.name+"<br/>"+'提交的年龄是:'+parsePostDate.age);
           res.end();
       });
     }
     else{
         res.writeHead(404,{"content-type":'text/plain'});
         res.write('404,服务器未找到该页面!');
         res.end();
     }
}).listen(10000);

  

/**
 * Created by Administrator on 2016/8/24 0024.
 * filesUpload
 */
var http=require('http'),
    url=require('url'),
    fs=require('fs'),
    formidable=require('formidable');
    http.createServer(function(req,res){
    var reqPathName=url.parse(req.url).pathname
        if(reqPathName=='/index'){
            var resDate=fs.readFile('static/index.html','utf-8',function(err,data){
                if(err){
                    console.log('服务器错误,错误代码:007');
                }else{
                    res.writeHead(200,{'Content-Type':'text/html'});
                    res.write(data);
                    res.end();
                }
            })
        }else if(reqPathName=='/filesUpload'){
            var form = new formidable.IncomingForm();
            form.uploadDir = "./temp"; //设置文件上传的临时存放地,稍后可以fs.rename()重命名或移动. 默认的临时文件存放在os.tmpDir()
            //解析req提交的数据
            form.parse(req,function(err,fileds,files){ //fileds是提交的字符串数据,比如type="text",files是附件型的数据
                if(err){
                    console.log('提交出错!');
                }else{
                    res.writeHead(200,{'Content-Type':'text/html'});
                    console.log(fileds)//{ name: 'leyi', age: '10000' }
/*                    { attachment:
                        File {
                        domain: null,
                            _events: {},
                        _eventsCount: 0,
                            _maxListeners: undefined,
                            size: 22005,//附件大小
                            path: 'temp\upload_512b6849c0b21064f6abd98af736f9b3', //临时文件目录
                            name: 'about_us_bg.jpg',//附件名称
                            type: 'image/jpeg',//附件类型
                            hash: null,
                            lastModifiedDate: Wed Aug 24 2016 22:52:17 GMT+0800 (中国标准时间),//附近最后改动日期
                            _writeStream:
                        WriteStream {
                            _writableState: [Object],
                                writable: true,
                                domain: null,
                                _events: {},
                            _eventsCount: 0,
                                _maxListeners: undefined,
                                path: 'temp\upload_512b6849c0b21064f6abd98af736f9b3',
                                fd: null,
                                flags: 'w',
                                mode: 438,
                                start: undefined,
                                pos: undefined,
                                bytesWritten: 22005,
                                closed: true } } }*/
                    console.log(files)
                    var tempPath=files.attachment.path //临时文件地址
                    fs.rename(tempPath,'./files/'+new Date().getTime()+files.attachment.name);//移动文件
                }
                res.end();
            })
        }
        else{
            res.writeHead(404,{"content-type":'text/plain'});
            res.write('404,服务器未找到该页面!');
            res.end();
        }


}).listen(9999);

  

原文地址:https://www.cnblogs.com/leyi/p/5805166.html