我有一个用 Express/Nodejs 编写的后端应用程序,以及前端的 angularjs(我知道旧)。我正在尝试在 Electron 窗口中打开此应用程序。搜索后我发现了一些东西,这就是我想出的。
main.js
const {BrowserWindow} = require('electron')
const path = require('path')
require('./server/app.js')
function start() {
mainWindow = new BrowserWindow({
width: 800,
height: 600
});
mainWindow.loadURL(`http://127.0.0.1:8009/`);
mainWindow.focus();
};
app.on('ready', start)
./server/app.jsvar open = require('open');
var path = require('path')
var express = require('express');
var bodyParser = require('body-parser');
var api = require('./routes/api')();
var fs = require('fs')
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static(__dirname + '/../client'));
app.get('/api/getPoints/:instance', api.getPoints);
app.get('/api/startDataCollection/', api.startDataCollection);
app.get('/api/getEllipses/:instance', api.getEllipses);
app.get('/api/getOutliers/:instance', api.getOutliers);
app.get('/api/getLabels/:instance', api.getLabels);
app.get('/api/getDescription/', api.getDescription);
app.post('/api/postDescription/', api.postDescription);
app.post('/api/postLabel/', api.postLabel);
app.listen(8009, function () {
console.log('Server listening at http://127.0.0.1:8009');
});
我通过要求 app.js 模块来启动服务器。该应用程序正常启动,我可以在端口 8009 访问它,但 Electron 窗口没有弹出。
最佳答案
app
不是声明的变量。你可以试试:
setTimeout(() => start(), 3000);
这将在 Node 启动 3 秒后手动启动 Electron 窗口。
如果可行,请导出
app
从您需要的模块。module.exports.app = app;
关于node.js - 用 Electron 打开一个 Express 应用程序(Nodejs),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61549474/