Home > Back-end >  How to send data (a url string) in express to backend from frontend?
How to send data (a url string) in express to backend from frontend?

Time:01-20

I am trying to build a wikipedia web scraper api and this is my code:

const app = express()
const port = 3000

const url = "https://en.wikipedia.org/wiki/Yeti_Airlines_Flight_691"

axios.get(url).then(async (res) => {
    try {
        if (res.status == 200) {
            const result = // Doing cheerio stuff here
            app.get('/', (req, res) => {
                res.status(200).send(result)
            })
            app.listen(port, () => {
                console.log(`Example app listening on port ${port}`)
            })
        }
    } finally {
        //
    }
});

How can I send url dynamically to backend using express and do some stuff then send result back to frontend?

CodePudding user response:

Client side:

This is a function you define in your frontend. You set a request link, which must be known here on client side and server side. Thus take something like /request. Then use axios to send a request to the server. You can pass any parameters with „dynamic“ Information as you called it. The server will receive these informations and can handle them.

const getData = () => {
// you can set the link to whatever you want
    const requestLink = `/request`;
    const parameters = { url: www.wikipedia.de/something };

    axios
      .get(requestLink, {params: parameters})
      .catch((error) => {
      // here you can implement error handling
        console.log(error);
      })
      .then((res) => {
      // print result, received from the server
        console.log(res);
      });
  };

Server side:

The backend is going to wait for a request to the defined link /request. If he receives such a request, he is executing the code. The req variable contains your dynamic data, such as the wiki url.

Use res.json to send data back to your frontend.

app.get(`/request`, (req, res) => {
     // do something with the request here
     console.log(req);
     // send result to the frontend
     res.json({
       status: "This could be an answer to the frontend.",
     });
    });
  • Related