Hello! have anyone faced an issue where your dev e...
# 🌎general
f
Hello! have anyone faced an issue where your dev env works fine but in production the HTTP(Axios) calls fail like this in the logs:
Actions [Error: connect ECONNREFUSED 127.0.0.1:80]
?
e
Hey @fierce-cpu-1068, that's odd! Are you sure you are using the right variables in production?
Because 127.0.0.1 is the localhost IP, so maybe you didn't update?
f
@early-train-33247 Im quite sure. I checked like 5 times
Im going to try making my own API. That way ill have more control over connecting to external services
and all the pros of my own code editor and keep it simple in botpress
I think supabase for some reason is rejecting the request on production
e
You could build a middleware in this case. All your Execute Code cards would make requests to a Node.js server which calls APIs and always answers in the same format. The request to the server could be like:
Copy code
json
{
  "action": "new-user",
  "data": {
    "userName": "Henry"
    ...
  }
}
And the answer:
Copy code
json
{
  "messages": ["Sign up successfull", "Now log in please"]
  "parameters": {
    "userId": "1234"
  }
}
In fact, I have a middleware like this, let me share my router:
Copy code
js
export async function AppRouter(req: Request, res: Response) {
    const request: BotpressRequest = req.body;

    console.log('[BOT-MIDDLEWARE]:[app]: 💬 REQUEST.BODY: ', request);

    const response: BotpressResponse = {
        messages: [],
        parameters: {},
    };

    if (!request || !request.action) {
        return res.status(400).send('Bad Request');
    }

    try {
        const action = request.action;

        if (action === 'new-user') {
            await newUser(request, response);
        } else if (action === 'login') {
            await login(request, response);
        } else {
            await fallback(request, response);
        }

        console.log(
            `[BOT-MIDDLEWARE]:[app]: ✅ ACTION ${request.action.toUpperCase()} EXECUTED SUCCESSFULLY`
        );

        console.log('[BOT-MIDDLEWARE]:[app]: 💬 ANSWER : ', response);

        res.status(200).send(response);
    } catch (error: any) {
        console.log(
            `[BOT-MIDDLEWARE]: ❌ ERROR WHILE EXECUTING ACTION ${
                request.action
            } : ${handleError(error)}`
        );

        console.log('[BOT-MIDDLEWARE]:[app]: 💬 ANSWER : ', {
            error: handleError(error),
        });

        res.status(400).send({
            error: handleError(error),
        });
    }
}
Now you would need to install the axios and @cold-motherboard-82208/client libraries to give you the interface types
f
thats really interesting thnks
e
Hey @fierce-cpu-1068, i created a Github Repo which you can download and use as your own middleware
You would still need a server if you want to manage a database etc, but this middleware allows you to connect to that server and many others
This file botpress-inline.ts contains the snippets from Execute Code cards. It's not executed in the middleware, it's just so you know in a glance what Botpress is doing You should make your Codes like them in order to get standardized behavior (starting in the try catch)
2 Views