https://discord.gg/botpress logo
#🌎general
Title
# 🌎general
f

fierce-cpu-1068

07/05/2023, 4:26 PM
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

early-train-33247

07/06/2023, 7:58 PM
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

fierce-cpu-1068

07/07/2023, 1:19 PM
@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

early-train-33247

07/08/2023, 3:39 PM
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

fierce-cpu-1068

07/09/2023, 1:13 AM
thats really interesting thnks
e

early-train-33247

07/11/2023, 12:53 AM
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