NodeJS - 用户登录前无法访问文件系统

时间:2018-02-24 15:11:34

标签: node.js centos fs pm2 listdir

我有一个节点服务器应用程序,在CentOS上作为pm2服务运行。 它必须列出具有nodejs fs 模块的所有外部驱动器。当root用户登录时,它可以正常工作。重启操作系统后,pm2再次运行服务器,但无法访问查看目录。

这是列出所有目录的代码:

const driveList = require('drivelist');
const fs = require('fs');
const path = require('path');
const emptyDir = require('empty-dir');

const config = require('../config.json');


exports.list_all_drives = function(req, res) {
    return driveList.list((err, drives) => {

        if(err) return res.status(500).send('there was an error');

        let isWin = /^win/.test(process.platform);

        let usbDrives = [];
        drives.map((drive) => {
            if(isWin){
                for(let index in drive.mountpoints){
                    usbDrives.push(drive.mountpoints[index].path);
                }
            }else{
                if(!drive.system){
                    for(let index in drive.mountpoints){
                        usbDrives.push(drive.mountpoints[index].path);
                    }
                }
            }
        });

        if(err) return res.status(500).send('there was an error');
        let records = [];
        usbDrives.forEach((name, i) => {
            let record = {};
            record.id = i;
            record.title = name;
            record.path = name;
            record.isDirectory = true;

            let skip = false;
            config.skipPath.forEach((val) => {
                if(record.path == val){
                    skip = true;
                }
            });

            if(!skip){
                records.push(record);
            }
        });
        res.status(200).send(records);
    });
};

当用户登录时,功能工作并列出所有操作系统目录,但系统重新启动时返回500错误:

  

{ “错误号”: - 2 “码”: “ENOENT”, “系统调用”: “打开”, “路径”: “/运行/用户/ 0 / drivelist-ecc75a4a9523.sh”}

0 个答案:

没有答案