|
var http = require ( 'http' ) ;
http.createServer (
function handler ( req , res ) { res.writeHead (
200 , {'Content-Type' : 'text/html ; ' });if (req.url !== '/favicon.ico') {
str
= "" ; //随机字符 - 20k //随机生成文件
fileName = String.fromCharCode ( Math. floor ( 65 + ( Math. random () * ( 122 - 65 )))) + ".txt" ;
//str 赋值
for ( i = 0; i < 200000; i++ ){
n = Math. floor ( 65 + ( Math. random () * ( 122 - 65 )) ) ;
str += String. fromCharCode ( n ) ;
}
//写入
var fs = require ( 'fs' ) ;//操作文件模块
//写入内容
fs.writeFile ( fileName,str,function ( err, fd ) {
if ( err ) throw err ; //如果错误则抛出错误
//读取文件 并展示的页面
fs.readFile ( fileName , function( err, data ){
if ( err ) throw err ;
res.write(data);//输出
res.end ('') ; // 结束
}) ;
}
);
}
}).listen(8000) ;
console. log ( 'success:8000' ) ; |
|
|