var http=require(‘http‘); var url=require(‘url‘); var fs=require(‘fs‘); var silly=require(‘silly-datetime‘); var formidable=require(‘formidable‘); var exec = require(‘child_process‘).exec,child; /*child=exec(‘mv ‘+file.File.path+‘ ‘+file.File[‘name‘],function(err,out){ console.log(out); //命令增删改查; linux命令 err && console.log(err); })*/ console.log(silly.format(new Date(),‘YYYY-MM-DD‘ )); //日期格式化插件; //console.log(foo); var server=http.createServer(function(req,res){ res.writeHead(200,{"Content-Type":"text/html;charset=utf-8"}); var form=new formidable.IncomingForm(); //表单上传插件; console.log(req.method); //获取请求方法类型; form.uploadDir="./uploads"; //定义上传路径 上传上来的没有后缀; form.parse(req,function(err,fields,file){ if(err){ throw err; } console.log(fields); console.log(file); console.log(file.File); var oldname="./"+file.aa.path; var newname="./uploads/"+file.aa[‘name‘]; fs.rename(oldname,newname,function(err){ if(err){ //上传文件改名 throw err; } }) res.writeHead(200,{‘content-type‘:‘text/plain‘}) res.end("success"); }) }); server.listen(3000,"127.0.0.1"); console.log(‘wanbi‘); /*fs.readFile("./index.html","UTF-8",function(err,data){ if(err){ throw err; } res.end(data); }); fs.readFile("./index.html","utf-8",function(err,data){ if(err){ res.end(‘aaachucuo‘); return ; } res.end(data); }) */
原文:http://www.cnblogs.com/gjKai/p/6720250.html