Austin
Austin

Reputation: 345

Starting and stopping node app from inside Electron app

I am trying to be able to start and stop another node.js application from inside of my electron app.

I have done the following so far which is working to start the node at from bot.js when I run npm start to spin up the electron app:

In main.js:

var bot = require('./Bot/bot')
, server = require("./server");

And in server.js:

require("http").createServer(function (req, res) {
res.end("Hello from server started by Electron app!");
}).listen(9000)

Now if I wanted to stop the application at bot.js from inside the electron app (or start it again), from something like a button click, I'm not sure how to do that.

Upvotes: 1

Views: 1713

Answers (1)

Tom Hightower
Tom Hightower

Reputation: 40

Server.close() I think is what you're looking for.
.listen() returns an http.Server instance, which this method can be called on:

const http = require('http');
const server = http.createServer(function (req, res) {
    res.end("Hello from server started by Electron app!");
}).listen(9000);

// To Close:
server.close();

You can then call server.listen() again to reopen the connection. Note that if the server is already open when you try to listen or not listening and you try to close it.
See the Server.close() documentation for more info!

Upvotes: 1

Related Questions