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

63 lines
2.0 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}test.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}test.desktop`);
const shortcutFileContent = `[Desktop Entry]
Type=Application
Name=${shortcutName}
Exec=xdg-open ${shortcutURL}
Icon=application-icon`;
fs.writeFile(shortcutFolderPath, shortcutFileContent, (err) => {
if (err) {
console.error(err);
res.status(500).send('安装失败!');
} else {
res.status(200).send('安装成功!');
}
});
});
// 运行项目 需要进入到当前的文件夹目录打开终端 执行 node nodeService.js
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});