2024-10-21 23:45:54 +08:00

48 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { app, BrowserWindow } from 'electron';
import express, { Request, Response } from 'express';
import morgan from 'morgan';
import cors from 'cors';
import { loadView, saveView, saveViewAs } from './save-view';
import { createWindow } from './windows';
const corsOptions = {
// 一些旧版浏览器(如 IE11、各种 SmartTV在 204 状态下会有问题
optionsSuccessStatus: 200
};
const backendApp = express();
backendApp.use(express.json());
backendApp.use(cors(corsOptions));
backendApp.use(morgan('dev'));
backendApp.get('/', (req: Request, res: Response) => {
res.send('<h1>Hello, World!</h1><br><img src="https://picx.zhimg.com/v2-b4251de7d2499e942c7ebf447a90d2eb_l.jpg"/>');
});
backendApp.post('/save-view', saveView);
backendApp.post('/save-view-as', saveViewAs);
backendApp.post('/load-view', loadView);
const PORT = process.env.PORT || 3000;
backendApp.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
// 下面注册 electron 窗口
app.on('ready', () => {
createWindow();
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});