且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

尝试从桌面读取节点js的文件时出错

更新时间:2023-02-02 07:49:47

由于安全原因,Web服务器被限制为当前目录。所以如果你想到达一些文件,首先移动到Web服务器的目录。只是为了证明这个概念你可以尝试这个代码:

Because of the security reasons the web server is limited to it's current directory. So if you want to reach some file move it first to the web server's directory. Just to prove the concept you could try this code:

app.get('/aa', function (req, res) {
    var fs = require('fs');
    fs.readFile(__filename, 'utf8', function (err, data) {
        if (err) {
            return console.log(err);
        }
        console.log(data);
        res.send(data);
    });
});

它将显示代码所在文件的内容。

It will display the content of the file where the code is.