下面是我的代码
var http = require('http');
var fs = require('fs');
var arguments = process.argv;
var path = process.argv[3];
var path2 = arguments[3];
var server = http.createServer(
function (req, res)
{
console.log('path1'+path);
console.log('path2'+path2);
console.log('path3'+arguments[3]);
var fileStream = fs.createReadStream(arguments[3]);
fileStream.pipe(res);
}
);
server.listen(arguments[2]);
如果我将路径或路径 2 传递给 fs.createReadStream(),我的代码可以工作,但如果我传递参数 [3],它会失败,console.log(path3) 也会打印 undefined。我不明白这一点。有人请解释。谢谢。
最佳答案
恭喜你发现了JS的函数对象
的参数属性
请引用MDN Docs了解更多
The arguments object is an Array-like object corresponding to the arguments passed to a function.
试试 console.log(arguments)
或者
var http = require('http');
var fs = require('fs');
var parguments = process.argv;
var path = process.argv[3];
var path2 = parguments[3];
var server = http.createServer(
function (req, res)
{
console.log('path1'+path);
console.log('path2'+path2);
console.log('path3'+parguments[3]);
var fileStream = fs.createReadStream(parguments[3]);
fileStream.pipe(res);
}
);
server.listen(parguments[2]);
请注意,我已将 arguments
的名称更改为 parguments
关于javascript - Learnyounode教程#11 HTTP文件服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33242534/