You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to figure out how to read a file from the file system in a Gatsby Function. The code I have now works locally without problem, but not when up on the server. I assume it's because the server only have the built files. To make matters worse, I don't really want to put it in static or anywhere public, as it's a certificate file for server-to-server API calls.
This is my current code that works locally:
import { GatsbyFunctionResponse } from "gatsby"
import fetch from "node-fetch"
import https from "https"
import fs from "fs"
import path from "path"
export default function handler(
req: null,
res: GatsbyFunctionResponse
) {
const pfx = fs.readFileSync(path.resolve(__dirname, `../../cert/myCertFile.p12`));
const passphrase = `abc123`;
const agent = new https.Agent({ pfx, passphrase });
fetch(`https://securedapi.com/`, {
agent
})
.then(response => response.json())
.then(response => {
res.status(200).json({
response
})
}).catch(error => {
res.status(500).json({
error
})
})
}
However, I can't seem to get the file when hosted on Netlify at all - have tried all sorts of paths to it.
Any ideas on how I can make it work? I tried to also read the file in as a new node, but then I get error messages about Webpack not being able to parse the file instead.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I am trying to figure out how to read a file from the file system in a Gatsby Function. The code I have now works locally without problem, but not when up on the server. I assume it's because the server only have the built files. To make matters worse, I don't really want to put it in
staticor anywhere public, as it's a certificate file for server-to-server API calls.This is my current code that works locally:
However, I can't seem to get the file when hosted on Netlify at all - have tried all sorts of paths to it.
Any ideas on how I can make it work? I tried to also read the file in as a new node, but then I get error messages about Webpack not being able to parse the file instead.
Beta Was this translation helpful? Give feedback.
All reactions