javascript - Learnyounode教程#11 HTTP文件服务器

标签 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.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/

相关文章:

javascript - 使用 ORM Sequelize/node.js 操作数据

node.js - mongodb 神秘错误 process.nextTick(function() { throw err; }) ^ Object 错误。<anonymous>

javascript - 为动态对象定义编写更清晰的代码

javascript - 我无法让光谱颜色选择器工作。我缺少什么?

javascript - 使用头部图像文件修改 HTML5 贪吃蛇游戏

node.js - fs watch方法的区别

node.js - 具有 2 个集合的 Mongodb 聚合

node.js - 具有动态引用的 Mongoose 事件模型

javascript - 我的 JavaScript API 应该在无效输入时抛出或返回值吗?

javascript - 多个id上的jquery选择器