Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

306
Visualizações
How to correctly call a function from Node.JS to Express.JS

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}`)
    })
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda