Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Learnyounode教程#11 HTTP文件服务器_Javascript_Node.js - Fatal编程技术网

Javascript Learnyounode教程#11 HTTP文件服务器

Javascript Learnyounode教程#11 HTTP文件服务器,javascript,node.js,Javascript,Node.js,下面是我的代码 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

下面是我的代码

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]);

如果我将path或path2传递给fs.createReadStream(),我的代码会工作,但如果我传递参数[3],它会失败,console.log(path3)也会打印未定义的。我不明白。请有人解释一下。谢谢。

祝贺您刚刚发现了JS的
函数对象的
属性的参数请参考了解更多信息

arguments对象是与传递给函数的参数相对应的类似数组的对象

尝试
console.log(参数)

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]);

请注意,我已将
参数的名称更改为
parguments

path2
打印正确的输出?您好,又是您,感谢您的帮助!显然,在JS基础课程中,我还有很多东西要学。
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(Number(process.argv[2]));