【发布时间】:2023-04-06 22:29:02
【问题描述】:
我不确定我这样做是否正确。我的最终目标是从 lambda 函数向我的电子应用程序发送一个发布请求并创建一个系统通知。在本地我已经能够从邮递员那里做到这一点,但是当我安装应用程序(在 linux 上)它不起作用,现在我不确定我应该将我的请求指向哪里,在开发中我也指出了它。 http://localhost:3000/notify 安装应用程序时会发生什么。我将如何向应用程序发送发布请求,最终我想建立用户帐户,所以我需要根据 lambda 逻辑向每个单独的用户发送请求。
我正在使用电子快递,有没有另一种方式来处理帖子请求。
这是我的 main.js 文件中的代码
"use strict";
const { app, BrowserWindow } = require("electron");
const { Notification } = require("electron");
const express = require("express");
const bodyParser = require("body-parser");
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
},
});
win.loadFile("index.html");
win.webContents.openDevTools();
}
app.whenReady().then(createWindow);
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
// Create a new instance of express
const appE = express();
// Tell express to use the body-parser middleware and to not parse extended bodies
appE.use(bodyParser.json());
// Route that receives a POST request to /sms
appE.post("/notify", function (req, res) {
const body = req.body;
console.log(body);
res.set("Content-Type", "text/plain");
function showNotification() {
const notification = {
title: "Basic Notification",
body: `You sent: ${body.message} to Express`,
};
new Notification(notification).show();
}
app.whenReady().then(createWindow).then(showNotification);
res.send(`You sent: ${body.message} to Express`);
});
// Tell our app to listen on port 3000
appE.listen(3000, function (err) {
if (err) {
throw err;
}
console.log("Server started on port 3000");
});
【问题讨论】:
-
所以你的网络服务器在本地,但是一旦你将它部署到服务器上它就不再工作了?你是如何发布到你的服务器的?您能否提供您在本地环境和服务器环境中所做的工作的分步过程?
-
我不确定我是否理解,我没有部署应用程序,它是一个电子应用程序,所以它安装在操作系统上。我只是向邮递员发送一个帖子请求,其中包含一条消息到 localhost:3000。我想知道的是如何实现 aws lambda 函数和内置电子应用程序之间的通信,因为它安装在计算机上并且没有 URL,我也可以发送帖子请求。
标签: javascript node.js express electron