How to serve static files from an asar archive
Asked Answered
S

1

9

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?

Salvia answered 25/3, 2016 at 14:15 Comment(2)
Did you find a workaround to serve static files from asar? I'm having the exact same problem..Tussore
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.Selfinduction
X
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

Xyloid answered 22/3, 2019 at 10:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.