how do i reference the functions defined in my server.js from app.js file, in my custom…
- User
how do i reference the functions defined in my server.js from app.js file, in my custom fresh desk app with platform version 2.3
- Assistant
Assuming that your server.js file runs on a separate server, you cannot directly reference its functions from your Freshdesk app.
However, you can expose your server.js functions using an API or a web service. You can then make API calls or use Ajax requests from your app.js file to invoke these functions.
Here's an example of how you can expose a function from your server.js file as a REST API using Express.js:
code// server.js const express = require('express'); const app = express(); function myFunction(param) { // function logic here } app.get('/my-api', (req, res) => { const param = req.query.param; const result = myFunction(param); res.json(result); }); app.listen(3000, () => console.log('Server running on port 3000'));In your app.js file, you can use the
fetchmethod to make an API call to this endpoint: - User
no my server.js is not running on a seperate server. it is just a file within the same project.
- Assistant
If your server.js file is within the same project as your Freshdesk app and both files are written in Node.js, you can export the functions from server.js and import them in app.js using the
requiremethod.Here's an example of how you can export a function from server.js:
code// server.js function myFunction(param) { // function logic here } module.exports = { myFunction, };In your app.js file, you can import the function from server.js and use it as follows:
code// app.js const server = require('./server.js'); const result = server.myFunction(param);Make sure that the path to server.js is correct and that you are exporting and importing the correct function names.
- User
but in freshdesk custom app, require is only allowed in server.js, i need to know the client property using which i can invoke the function
- Assistant
If you need to access a function defined in server.js from app.js in a Freshdesk custom app, you can define a custom endpoint in server.js and make a request to it from app.js using the Freshdesk API's
requestmethod.In server.js, define a custom endpoint that invokes the required function and returns the result:
code// server.js function myFunction(param) { // function logic here } app.get('/custom-endpoint', function(req, res) { const param = req.query.param; const result = myFunction(param); res.send(result); });In app.js, make an API request to the custom endpoint using the
requestmethod:code// app.js fd.request.get('/api/v2/apps/your-app-id/custom-endpoint?param=value') .done(function(data) { // handle response data here }) .fail(function(error) { console.error(error); });Make sure to replace with the actual ID of your Freshdesk app.