highcharts / node-export-server

Highcharts Node.js export server
Other
356 stars 260 forks source link

Load local .js and .css files when using the server (enhancement/puppeteer) #366

Open m-rm opened 2 years ago

m-rm commented 2 years ago

We would like to load additional local resources when using the server, but it seems that only the "server" section of the configuration is used while starting the server. During the actual HTTP requests, nothing in "customCode" is applied. Is this the case due to security reasons or is this feature not complete yet?

iherbak commented 12 months ago

Same issue here. By security reasons allowFileResources is always false on http request, but how could we include js files then?

Thanks

m-rm commented 12 months ago

I found a workaround that works for my use case: In addition to highcharts-export-server I start an express server that is able to serve my JS files and only binds to localhost. I then use the scripts section of the node-export-server config to load the scripts.

Here's my express server code:

const express = require('express')
const cors = require('cors');
require('dotenv').config()

const app = express()
const port = process.env.HC_STATIC_PORT || 3000

app.use(cors({
    origin: '*' // We need CORS headers because otherwise the Pupeteer browser instance will block the files. Using localhost as origin doesn't work. Since we only bind to localhost * should be ok
}));

app.use(express.static('static'))

app.listen(port, 'localhost', () => {
  console.log(`Highcharts Node.js export server static file app listening on port ${port}`)
})

One important side note: the scripts are combined and cached. If you change them the cached version is not updated. You need to delete node_modules/highcharts-export-server/.cache to update the scripts. Also don't forget to add moment and lodash to the scripts section, because it's there by default, but if you define it it's overwritten.

If you need to load CSS files it's possible to also host them locally using the express server and then load them through JavaScript using the customCode request parameter.