Sunday 8 November 2020

What is the url where an express app is located at inside an electron app

I am not sure I am doing this quite right. My end goal is to send a post request from a lambda function to my electron app and create a system notification. Locally I have been able to do it from post man, but when I install the app (on linux) It doesn't work, now I am not sure where I am supposed to point my request to, in development I pointed it too. http://localhost:3000/notify what happens when you install the app. How would I send a post request to the app, eventually I want to build user accounts, so I will need to send requests to each separate user based on the lambda logic.

I am using express with electron, is there another way to handle post requests.

Here is my code so far in my main.js file

"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");
});


from What is the url where an express app is located at inside an electron app

No comments:

Post a Comment