Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Express inside Atom Electron

I have an application running Express, and I am trying to distribute it using electron.

Running electron in debug with this:

/path/to/electron/Electron.app/Contents/MacOS/Electron path-to-my-app

My application runs perfectly fine. Express fires up its server and everything works -- the main window opens correctly using mainWindow.loadUrl('http://localhost:3000/');

When I follow the distribution tutorial (linked before) I copy my application resources to:

/path/to/electron/Electron.app/Contents/Resources/app

But now when I run Electron.app, I see Cannot GET / in the main window... but I have no idea why.

Any ideas?

My only thought is that process.cwd() is not correctly helping me define the document root here:

//configure Express to default web requests to /workspace/ folder
expressApp.use(express.static(process.cwd() + '/workspace'));

But if that's the case, I don't know how to get around it.

like image 481
arthurakay Avatar asked Apr 24 '15 21:04

arthurakay


People also ask

Can we use express in electron JS?

You can run your Express app very easily inside your Electron app. All you need to do is to: place all the files of your Express app inside a new app folder in your_electron_app\resources\app.

Does electron include node JS?

Electron is a framework for building desktop applications using JavaScript, HTML, and CSS. By embedding Chromium and Node. js into a single binary file, Electron allows you to create cross-platform apps that work on Windows, macOS, and Linux with a single JavaScript codebase.


2 Answers

It turns out that express for some reason didn't like my document root mapping.

Rather than using:

//configure Express to default web requests to /workspace/ folder
expressApp.use(express.static(process.cwd() + '/workspace'));

I instead use this:

expressApp.use(express.static(path.join(__dirname, 'workspace')));
like image 53
arthurakay Avatar answered Oct 29 '22 14:10

arthurakay


Don't use process.cwd, use process.resourcesPath instead.

like image 34
Ana Betts Avatar answered Oct 29 '22 13:10

Ana Betts