Use npm at server level

Hi,

I am playing with logging api calls using something called treblle.com (the client has seen this and wants it as it “looks cool”) :frowning:

It sits at the server level rather than page or server connect and I have installed the npm but there is a little bit of code that needs to be added so it can capture API calls and the data.
If I add it to the server.js it all works fine but the code is sometimes removed from that file (I assume on updates or similar).
Where should I put it so it runs and collects API calls directly on the server?

Here is the code from their instructions:

// DON'T FORGET TO LOAD THE REQUIRED JS MODULES
const express = require("express");
const { useTreblle } = require("treblle");
 
const app = express();
app.use(express.json());
 
useTreblle(app, {
    apiKey: "_YOUR_API_KEY_",
    projectId: "_YOUR_PROJECT_ID_"
});

When I add it to the server.js I edit it to:

const { useTreblle } = require("treblle");

useTreblle(app, {
    apiKey: "_YOUR_API_KEY_",
    projectId: "_YOUR_PROJECT_ID_"
});

Where should this go if not in the server.js?

Thanks.

Has anyone else needed to add code like this or have any recommendations?

I currently have to add the code manually quite often (Wappler rewrites the server.js file often.

Search for middleware extensions in the forum. Specifically a Sentry one. It’s similar.

1 Like

Thanks JonL, as always, spot on comments.

For anyone interested I followed the solution in this post -
https://community.wappler.io/t/how-to-install-sentry-on-the-back-end-nodejs/31987/19

Specifically creating a new js file in extensions/server_connect/routes/treblle.js containing the treblle code that I previously added to the server.js (in my post above) but wrapping it with “exports.before = function (app) { };” to get:

const { useTreblle } = require("treblle");

exports.before = function (app) {
    useTreblle(app, {
        apiKey: "_YOUR_API_KEY_",
        projectId: "_YOUR_PROJECT_ID_"
    });
};

Now since I am not editing the server.js file this should remain in place after updates etc.

Obviously, follow the deployment instructions from treblle.com regarding installing the node package etc.

1 Like