To support running within an SFE or not,
Something like this should work.
const getStaticPath = () => {
if (process.pkg !== undefined) {
return join(dirname(process.argv0), 'static');
} else {
return join(__dirname, 'static');
}
}
httpStatic: {
path: getStaticPath(),
root: '/',
options: {
maxAge:'1d',
setHeaders:function(res,path,stat){
res.set('x-timestamp',Date.now())
if(path.endsWith("png")){res.setHeader('Cache-Control','public, max-age=86400')}
}
}
},
Explanation.
The SFE will treat relative paths as relative paths inside the VFS - so we actually need to use absolute paths.
The if (process.pkg !== undefined)
block just checks if we should use the path that the executable its self is run from, or from the uncompiled script location.
process.argv0
is the SFE that was launched.
You can see it happening here.
Whether the SFE should use an embedded flow or create a standard User Directory if not embedded (it create the user Direct next to the executable)
And to detect the correct prefix.
const pathPrefix = process.platform === 'win32' ? 'c:/' : '/';