首页 > 解决方案 > 快速应用程序位于电子应用程序内的 url 是什么

问题描述

我不确定我这样做是否正确。我的最终目标是从 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");
});

标签: javascriptnode.jsexpresselectron

解决方案


您的应用程序正在将 express 应用程序作为使用端口 3000 的子进程执行。

因此,每当用户运行此应用程序时,快速服务器将托管在他们的本地计算机上,并将监听来自端口 3000 的端口请求。

但不幸的是,您无法向此应用程序发出 post 请求。

为什么?他们没有使用服务器操作系统,这个应用程序将只能监听来自本地网络等的请求。总而言之,我认为你的方式有点错误。同样,我想知道是否会有人打开他们的端口来接收来自任何地方的请求。因为这对用户来说风险太大......


推荐阅读