0

I want to fetch some html via nodejs and fetch the output in my browser , So I used following code

const express = require('express')
const app = express()
const fetch = require("node-fetch");
const port = 3000


app.listen(port, () => console.log(`Example app listening on port ${port}!`));

fetch("https://example.com/results.php")  .then(res => res.text())
  .then(data => obj = data)
  .then(() => 
  app.get('/', (req, res) => res.send(obj))

  )

Then I started the app using node app

Now When i run localhost:3000 , this gives same output everytime, But https://example.com/results.php is dynamic results page , which returns various result on every reload.

So what i need is Everytime I open localhost:3000 , it must fetch the url again and return new result in browser window , Sorry I am completely new to nodejs , I am just trying to remake php curl from nodejs.

1 Answer 1

4

You need to put the fetch login in the GET route.

Think about it logically. What you want to do is:

When user requests the / page, fetch "https://example.com/results.php" and send the result to the user.

The / route thus has to be available at all times, and when it gets hit you fetch the required resource. Here is how it translates:

const express = require('express');
const app = express();
const fetch = require("node-fetch");
const port = 3000;


app.get("/", (req, res) => {
    fetch("https://example.com/results.php")
      .then(res => res.text())
      .then((obj) => {
        res.send(obj);
      })
})


app.listen(port, () => console.log(`Example app listening on port ${port}!`));
Sign up to request clarification or add additional context in comments.

Comments

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.