8

I am trying to build an electron app with a server component using express for remote control.

The express module initializes with

var staticPath = path.resolve('app/assets')

setupNotifications(server);

app.use(cors());
app.use('/api/', json());
app.use('/api/', router);
app.use('/assets', express.static(staticPath));
console.log('serving static files from : ' + staticPath);

the Elecron docs mention that the current working directory cannot be set to a folder in the asar archive. This is confirmed by the error message indicates the file is being tried to be read from 'app/assets' below the root folder of the installation.

How can I tell express to read the files from the ASAR archive? And how can I do this so that I can develop with an 'exploded' archive and deploy with a built archive?

p u
  • 1,395
  • 1
  • 17
  • 30
Peter Tillemans
  • 34,983
  • 11
  • 83
  • 114
  • Did you find a workaround to serve static files from asar? I'm having the exact same problem.. – Genar May 05 '17 at 08:07
  • 1
    I had the same problem, I got around by including the app folder in the files and asarUnpack it: "build": { "files": [ "app/**/*" ], "asarUnpack":[ "app/**/*" ] } Then you can host from 'resources/app.asar.unpacked/app' But this solution is pretty ugly: app is integrated twice. I didn't manage to asarUnpack the normal version. – Diogyn Dec 17 '17 at 15:42

1 Answers1

0

I had the same problem. It worked for me using relative paths. The static files served by express were kept in the same directory as the file where express initializes. Below you can see a snippet of my working code:

const myStaticPath = './static'
app.use('/previewer', express.static(path.join(__dirname, myStaticPath)))

Cheers! Nicu

Nicu Timofte
  • 152
  • 1
  • 8