How to start a local server by click on a button in web browser

Viewed 287

I want to build a mock server project.

This idea is inspired by Mockoon (https://github.com/mockoon/mockoon)

You can start a local mock server by clicking a play button.

But it is made with Electron.

So I wonder whether it can be started by a web browser.

The mock server should be launched locally and configured by the input in the web.

Is it possible to make this? If it is, please give me some directions, thanks!

1 Answers

Indeed you cannot start a server from Electron's Chrome, but Electron also embarks Node.js. Behind the scene, Mockoon is using Express.js to launch a server, which is running on Node.js. You can see the code there. (Source: I am Mockoon creator :) )

Mockoon is also based on Angular, but it should be quite simple with a basic Electron setup. Something like this should do the trick:

import * as express from 'express';

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => res.send('Hello World!'));

app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`));

(This is taken from the getting started article).

If you don't want to use Electron, the setup would be the following:

  1. Create a Node.js application
  2. Create a route that serves a page with a button
  3. Clicking the button should call another specific route in the app (/start-server for example)
  4. on this route, Node.js could start an Express.js server as shown above (or any other server library)
Related