Expressjs inside electron, main or renderer?


#1

Is it possible to run an expressjs web server inside electron? when I try and run it in the main process I get errors where global.XMLHttpRequest object can’t be found, and in the renderer process I am getting prototype errors from importing express.

Any suggestions?


#2

I have an express router running fine in the main process.
The way I did it is probably not the best but it works for me. So, from my main.js I create a global object:

var ElJS = require('./ElecJS/ElecJS.js')
global.ElecJS = new ElJS(process.argv[3], process.argv[1].split(":")[2])

the relevant beginning of the ElecJS.js file looks like this:

var http = require('http');
var xprss = require('express')
var bodyParser = require('body-parser')

module.exports = function(lport, uiport) {
    //------------------------------
    //---Elecjs variables
    //------------------------------
    this.listeningPort = lport
    this.uiPort = uiport
    this.router = xprss()
    //------------------------------
    //---Elecjs methods
    //------------------------------
    this.Run = function(){
        // router utility functions/setups
        this.router.use(bodyParser.json());
        this.router.use(bodyParser.urlencoded({ extended: true }));
        //TODO: Routes come here
        this.router.post("/addMenuItem", addMenuItem)
        this.router.listen(this.listeningPort)
[...]

Hope this little example of mine gets you started.