0

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}`)
    })
6
  • since you made doParse to take in req and res, just pass in doParse as the callback. doParse would be acting as a modularized controller here Commented Feb 13, 2022 at 18:56
  • @mstephen19 Sorry, it's still not clear to me. I thought I was calling doParse as a callback before, but I saw that I have an arrow function. I don't know if it's correct? But I rewrote it to a regular function. 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. Commented Feb 13, 2022 at 19:18
  • Pleae show the code for 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 with doParse() since all it does is call runParser() and doesn't attempt to get a result back from runParser() and doesn't do anything with req or res. Commented Feb 13, 2022 at 20:08
  • @jfriend00 Thanks for the reply, I edited the source code in my question, and added parser.js there I also edited the code, the doParse function wasn't needed, it's a rudiment from my attempts to run everything correctly. Now I can call runParser without it. The req and res don't make any sense there either, so I removed them. Now I've trivialized the code to app.get('/', runParser) and pass the whole function as a callback, but it still doesn't return me anything Commented Feb 13, 2022 at 20:23
  • 1
    So, I can run your runParser() code just fine. It has a weirdness in that it uses plain callbacks (non-promise) for the fs.writeFile() such that the function's promise resolves before the fs.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? Commented Feb 13, 2022 at 21:20

1 Answer 1

1

All you need for Express is this:

const express = require('express');
const app = express();
const runParser = require("./parser");
const port = 3000;

app.get("/", (req, res) => {
   runParser().then(results => {
      res.json(results);
   }).catch(err => {
      console.log(err);
      res.status(500).send("error");
   });
});

app.listen(port, () => {
    console.log(`Server running on port ${port}`);
});

And, then you can access that either by just going to:

http://localhost:3000

from your local host or

http://yourdomain.com:3000

in the browser or by issuing an ajax call to the desired URL from webpage Javascript.


I wouldn't personally put this type of activity on a GET request to / because that can be hit by things like web crawlers, search engines, etc...

It probably belongs on a POST (so crawlers won't issue it) and I'd personally put it on some pathname such as:

app.post("/runparser", (req, res) => {
    // put your code here
});

And, then use a form submission or ajax call to that URL to trigger it.

Sign up to request clarification or add additional context in comments.

2 Comments

This is amazing, thank you very much. Everything turned out to be much easier than I thought. I don't know why, but I hardly saw the use of then in those materials where express was used. In any case, thanks, I will also take your comments about requests into consideration.
@blackenergy1645 - Since you're relatively new here, if this answered your question, you can indicate that to the community here by clicking the checkmark to the left of the answer. That will also earn you a few reputation points for following the proper procedure here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.