#node.js教程learnyounode 闯关答案
#ex1
console.log("HELLO WORLD");
#ex2
var sum =0;
for(var i =2;i<process.argv.length;i+=){
sum+=parseFloat(process.argv[i]);
}
console.log(sum);
#ex3
var fs = require(‘fs‘);
var pathname = process.argv[2];
//console.log(pathname);
var buf = fs.readFileSync(pathname);
var results = buf.toString().split("\n");
//console.log(results);
console.log(results.length-1);
#ex4
var fs = require(‘fs‘);
var pathname = process.argv[2];
fs.readFile(pathname,handle);
function handle(err,data){
if(err){
console.error(err);
return;
}
var results = data.toString().split("\n");
console.log(results.length-1);
}
#ex5
var fs = require(‘fs‘);
var path = require(‘path‘);
var pathname = process.argv[2];
var extname = process.argv[3];
fs.readdir(pathname,function(err,files){
for(var i=0;i<files.length;i++){
if("."+extname == path.extname(files[i])){
console.log(files[i]);
}
}
});
#ex6
##program.js
var mymodule = require("./mymodule");
var pathname = process.argv[2];
var extname = process.argv[3];
mymodule(pathname,extname,callback);
function callback(err,data){
if(err){
console.error(err);
return ;
}
data.forEach(function(file){
console.log(file);
});
}
##mymodule.js
var fs = require(‘fs‘);
var path = require(‘path‘);
module.exports = function (pathname,extname,callback){
var results = new Array();
var cnt =0;
fs.readdir(pathname,function(err,files){
if(err){
return callback(err);
}
for(var i=0;i<files.length;i++){
if("."+extname == path.extname(files[i])){
results[cnt++] = files[i];
}
}
//console.log(results);
callback(null,results);
});
};
#ex7
var http = require("http");
var url = process.argv[2];
http.get(url, function(res) {
res.setEncoding("utf-8");
res.on("data",function(data){
console.log(data);
});
});
#ex8
var http = require("http");
var url = process.argv[2];
http.get(url, function(res) {
res.setEncoding("utf-8");
var results ="";
res.on("data",function(data){
results +=data;
});
res.on("end",function(){
console.log(results.length);
console.log(results);
});
});
#ex9
#solution1:
var http = require("http");
http.get(process.argv[2],handle1);
http.get(process.argv[3],handle2);
http.get(process.argv[4],handle3);
var results= ["","",""];
function handle1(res){
res.setEncoding="utf-8";
res.on("data",function(data){
results[0] +=data;
});
res.on("end",function(){
//console.log(results[0]);
});
}
function handle2(res){
res.setEncoding="utf-8";
res.on("data",function(data){
results[1] +=data;
});
res.on("end",function(){
//console.log(results[1].toString());
});
}
function handle3(res){
res.setEncoding="utf-8";
res.on("data",function(data){
results[2] +=data;
});
res.on("end",function(){
console.log(results[0].toString());
console.log(results[1].toString());
console.log(results[2].toString());
});
}
#solution2:
var http = require("http");
var results=[];
var count =0;
for(var i=0;i<3;i++){
httpGet(i);
}
function httpGet(index){
http.get(process.argv[index+2],function(res){
res.setEncoding="utf-8";
results[index]="";
res.on("data",function(data){
results[index] +=data;
});
res.on("end",function(){
count++;
if(count>=3){
for(var i=0;i<results.length;i++){
console.log(results[i]);
}
}
});
});
}
##参考答案
var http = require(‘http‘)
var bl = require(‘bl‘)
var results = []
var count = 0
function printResults () {
for (var i = 0; i < 3; i++)
console.log(results[i])
}
function httpGet (index) {
http.get(process.argv[2 + index], function (response) {
response.pipe(bl(function (err, data) {
if (err)
return console.error(err)
results[index] = data.toString()
count++
if (count == 3)
printResults()
}))
})
}
for (var i = 0; i < 3; i++)
httpGet(i)
#ex10
var net = require("net");
var port = process.argv[2];
var server = net.createServer(function(socket){
socket.end(getTime()+"\n");
});
server.listen(port);
console.log("Server started http://127.0.0.1:%s",port)
function getTime(){
var date = new Date();
var year = date.getFullYear();
var month = justify(date.getMonth()+1);
var day = justify(date.getDate());
var hour = justify(date.getHours());
var minute = justify(date.getMinutes());
return year + "-"+month+"-"+day+" "+hour+":"+minute;
}
function justify(str){
return str<10?"0"+str:str;
}
#ex11
var http = require("http");
var fs = require("fs");
var server = http.createServer(function(req,res){
res.writeHead(200,{"content-type":"text/plain"});
fs.createReadStream(process.argv[3]).pipe(res);
});
server.listen(process.argv[2]);
#ex12
var http = require("http");
http.createServer(function(req,res){
var post = "";
req.on(‘data‘,function(chunk){
post += chunk;
});
req.on(‘end‘,function(){
post = post.toUpperCase();
res.end(post);
});
}).listen(process.argv[2]);
#参考答案:
var http = require(‘http‘)
var map = require(‘through2-map‘)
var server = http.createServer(function (req, res) {
if (req.method != ‘POST‘)
return res.end(‘send me a POST\n‘)
req.pipe(map(function (chunk) {
return chunk.toString().toUpperCase()
})).pipe(res)
})
server.listen(Number(process.argv[2]))
#ex13
var http = require("http");
var url = require("url");
http.createServer(function(req,res){
var url2 = url.parse(req.url,true);
var pathname = url2.pathname;
var iso = url2.query.iso;
var date = new Date(iso);
var result;
res.writeHead(200,{"Content-Type":"application/json"});
if(pathname == "/api/parsetime"){
result = {
"hour":date.getHours(),
"minute":date.getMinutes(),
"second":date.getSeconds()
};
res.end(JSON.stringify(result));
}else if(pathname == "/api/unixtime"){
result = {
"unixtime":date.getTime()
};
res.end(JSON.stringify(result));
}
}).listen(process.argv[2]);
时间: 2024-10-08 20:08:56