I have a completed script that acts as a parser. The script is written in NodeJS and it works properly. The script returns an array of data and also saves it to my computer. I would like to run this script from the frontend, at the click of a button. As far as I understand, I have to send a request to the server? It's suggested to use Express for the server, but I still haven't figured out how to call a third-party script from it, much less return any data from it. Right now all I want is for my script to run when I make a request for the root directory "/" and send me a json in response (or for example a json file)
const express = require('express')
const runParser = require("./parser");
const app = express()
const port = 3000
app.get('/', async (req, res,next) => {
await runParser()
next()
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
app.get('/', async function (req, res,next){ res.send(await doParse()) })But it still doesn't work. The function is called, I can see it in the console, but I can't get any data out of it and send it as an endpoint response.runParser(). If you're not getting data out of it, then it is apparently not properly returning the data you want - perhaps a problem with asynchronous coding. Plus, it's not clear at all what you're trying to do withdoParse()since all it does is callrunParser()and doesn't attempt to get a result back fromrunParser()and doesn't do anything withreqorres.app.get('/', runParser)and pass the whole function as a callback, but it still doesn't return me anythingrunParser()code just fine. It has a weirdness in that it uses plain callbacks (non-promise) for thefs.writeFile()such that the function's promise resolves before thefs.writeFile()is done. Other than that it works and resolves to a pretty large array of array of objects. So, what exactly is the problem you need help with?