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

66 lines
2.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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