我有一个节点服务器应用程序,它在 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 错误:
{"errno":-2,"code":"ENOENT","syscall":"open","path":"/run/user/0/drivelist-ecc75a4a9523.sh"}