I have built a simple web app using the LWR OSS(same as lwc-recipes-oss), I want to use the JSForce in this project to connect to Salesforce API. Because of the CORS policy, I am not able to fetch data from Salesforce as the browser is blocking it.
I know I need to use the jsforce-ajax-proxy
npm package so that I can redirect the requests through the server. This is something we can do with a plain express server.
// Simple Express server setup to serve for local testing/dev API server
const compression = require('compression');
const helmet = require('helmet');
const express = require('express');
const jsforceAjaxProxy = require('jsforce-ajax-proxy');
const app = express();
app.use(helmet());
app.use(compression());
app.all('/proxy/?*', jsforceAjaxProxy({ enableCORS: true }));
const HOST = process.env.API_HOST || 'localhost';
const PORT = process.env.API_PORT || 3002;
app.get('/api/v1/endpoint', (req, res) => {
res.json({ success: true });
});
app.listen(PORT, () =>
console.log(
`✅ API Server started: http://${HOST}:${PORT}/api/v1/endpoint`
)
);
But I don't want two servers, is there any way I can do this in the LWR server code as mentioned below?
import { createServer } from 'lwr';
createServer()
.listen(({ port, serverMode }) => {
console.log(`App listening on port ${port} in ${serverMode} mode\n`);
})
.catch((err) => {
console.error(err);
process.exit(1);
});