You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
xgd_system/nodeService.js

72 lines
2.4 KiB

const express = require('express');
const fs = require('fs');
const path = require('path');
const cors = require('cors');
const bodyParser = require('body-parser');
const app = express();
9 months ago
const port = 3000; // 可以根据需要修改端口号
app.use(cors());
app.use(bodyParser.json());
9 months ago
// window系统下创建快捷方式接口
app.post('/createShortcut', (req, res) => {
9 months ago
// folderPath存放的路径 shortcutName 快捷方式名称 shortcutURL 快捷方式的访问地址
const { folderPath, shortcutName, shortcutURL } = req.body;
9 months ago
// 检查是否缺少任何一个值
if (!folderPath || !shortcutName || !shortcutURL) {
return res.status(400).send('少了参数!');
}
9 months ago
// 创建文件夹(如果不存在)
9 months ago
if (!fs.existsSync(folderPath)) {
9 months ago
fs.mkdirSync(folderPath, { recursive: true });
9 months ago
}
9 months ago
// window下用.url
// const shortcutFilePath = path.join(folderPath, `${shortcutName}.url`);
// linux中用.desktop
9 months ago
const shortcutFilePath = path.join(folderPath, `${shortcutName}.desktop`);
9 months ago
// 删除旧的快捷方式文件(如果存在)
if (fs.existsSync(shortcutFilePath)) {
fs.unlinkSync(shortcutFilePath);
9 months ago
}
9 months ago
// window下创建快捷方式
// const shortcutFolderPath = path.join(folderPath, shortcutName + '.url');
// const shortcutFileContent = `[InternetShortcut]\nURL=${shortcutURL}`;
// linux下创建快捷方式
9 months ago
const shortcutFolderPath = path.join(folderPath, `${shortcutName}.desktop`);
9 months ago
// const shortcutFileContent = `[Desktop Entry]
// Type=Application
// Name=${shortcutName}
// Exec=/usr/bin/chromium-browser ${shortcutURL}
// Icon=/usr/share/icons/hicolor/48x48/apps/chromium-browser.png`;
// arm 下用火狐打开
9 months ago
const shortcutFileContent = `[Desktop Entry]
Type=Application
Name=${shortcutName}
9 months ago
Exec=sudo /usr/bin/firefox-esr ${shortcutURL}
Icon=/usr/share/icons/hicolor/48x48/apps/firefox-esr.png`;
9 months ago
try {
fs.writeFileSync(shortcutFolderPath, shortcutFileContent);
// 设置文件权限为可执行 0o755 表示 rwxr-xr-x 权限,即所有者可读写执行,其他用户可读执行
fs.chmodSync(shortcutFolderPath, 0o755);
res.status(200).send('安装成功!');
} catch (err) {
console.error(err);
res.status(500).send('安装失败!');
}
9 months ago
});
9 months ago
// 运行项目 需要进入到当前的文件夹目录打开终端 执行 node nodeService.js
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});