使用来自http.get的数据更新app.get响应

时间:2016-01-30 21:27:59

标签: javascript node.js express

我正在使用 express 并将路径作为url参数传递。

app.get("/download", function (req, res) {
    var location;
    var options = {
        host: 'example.com',
        port: 80,
        path: req.query.url.replace(/ /g, "%20")
    };

    http.get(options, function (res) {

        // get location from this 

        location = res.headers.location;
        console.log(location);

    }).on('error', function (e) {
        console.log("Got error: " + e.message);
    });

    // it won't get the location data since http.get is async
    // so how could I send this response using the data from http.get

    res.setHeader('Content-Type', 'text/html');
    res.send(location);
});

在此代码中,我希望从 http.get 的请求标头中检索数据,以便在浏览器上进行渲染。

无论如何,我可以在浏览器上呈现 http.get 数据。

2 个答案:

答案 0 :(得分:3)

你不能只移动函数内的 </head> <script> var sad = "1", sad2 = "2", sad3 = "3"; var n1 = "4", n2 = "5", n3 = "6", n4 = "7"; var h1 = "8", h2 = "9", h3 = "10"; var x = prompt("What is your mood from 1-10? 1 being sad, 10 being Happy."); if (x === sad || x === sad2 || x === sad3){ document.getElementByTagName("img").src = "sad.png"; document.getElementById("msg").innerHTML = "Sad."; document.getElementById("msg").href = "http://www.sad.com"; } else if (x === n1 || x === n2 || x === n3 || x === n4){ document.getElementByTagName("img").src = "sad.png"; document.getElementById("msg").innerHTML = "Neutral."; document.getElementById("msg").href = "http://www.neutral.com"; } else if (x === h1 || x === h2 || x === h3){ document.getElementByTagName("img").src = "happy.png"; document.getElementById("msg").innerHTML = "Happy."; document.getElementById("msg").href = "http://www.happy.com"; } </script> </head> <body style="text-align:center"> <img src="neutral.png"> <h1><a id="msg" href="">Waiting...</a></h1> </body> ,例如:

    List<Integer> ids = Arrays.asList(1, 2, 3, 4);
    Map<Integer, ResultSetFuture> futures = new HashMap<>();
    Map<Integer, List<DataPoint>> requiredMap = new HashMap<>();

    MappingManager manager = new MappingManager(session);
    /*
     * DataPoint has two attributes "private Date time;" and "private double value;"
     */
    Mapper<DataPoint> mapper = manager.mapper(DataPoint.class);

    for (Integer id : ids) {
        futures.put(id, (session.executeAsync("SELECT time, value FROM series WHERE seriesid = " + id)));
    }

    for(Integer id : futures.keySet()){
        ResultSet result = futures.get(id).getUninterruptibly();
        Result<DataPoint> dataPoints = mapper.map(result);
        requiredMap.put(id, dataPoints.all());
    }

我还建议使用request包来发出HTTP请求,因为它可以使整个过程更简单。

答案 1 :(得分:0)

您可以通过使用中间件将数据从“GET”查询传递到example.com。中间件将拦截对应用程序的任何请求,执行其中定义的任何操作,并将控制权传递回该请求的处理程序。

// Define middleware that makes a 'GET' request to 'example.com' and obtains the location.
app.use(function(req, res, next) {
  var options = {
      host: 'example.com',
      port: 80,
      path: req.query.url.replace(/ /g, "%20")
  };

  http.get(options, function (res) {

      // Attach the location to the request object. This will be present
      // in the request object of the 'GET' handler for the app.
      req.webLocation = res.headers.location;

      // Execute the next middleware or the route handler if there's no
      // other middleware.
      next();

  }).on('error', function (e) {
      console.log("Got error: " + e.message);
  });
});

app.get("/", function (req, res) {

    res.setHeader('Content-Type', 'text/html');
    // Render the 'webLocation' obtained from the middleware.
    res.send(req.webLocation);
});