简体   繁体   English

在Express中使用req.query与请求模块

[英]Using req.query in express with the request module

I am currently using Mikeal's request module in express. 我目前正在Express中使用Mikeal的请求模块。 I am trying to get a bunch of data from two json urls and put them into a table. 我正在尝试从两个json网址中获取一堆数据,并将它们放入表格中。 I want to be able to define two dates to use in the json url in req.query but I'm not too sure how. 我希望能够定义两个要在req.query的json url中使用的日期,但我不太确定如何使用。 Right now, I'm able to define the dates in my terminal but I want to get rid of that. 现在,我可以在终端中定义日期,但是我想摆脱它。 This is what I have so far. 到目前为止,这就是我所拥有的。

var request = require('request);
var link = https://examplelink.json;
var args = process.argv.splice(2);
var date1 = args[0];
var date2 = args[1];
var express = require('express');

var app = express();
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');    

app.get('/', function(req, res) {
    request.get({url: link + date1, json: true}, function(err, body, a) {
        if (err) {
            return res.send(500);
        }
        request.get({url: link + date2, json: true}, function(err, body, b) {
            if (err) {
                 return res.send(500);
            }
            res.render('table' { a: a, b: b});
       });
}):

var number = 7000;
app.listen(number);
console.log("Listening at " + number);

This is a simplified version but I hope it gets the message across. 这是简化版本,但我希望它能传达信息。 I would like to modify this so that I don't have to define the dates with args and I can do it in the url. 我想对此进行修改,这样就不必用args定义日期,并且可以在url中进行操作。

Thanks so much 非常感谢

You can specify the date arguments as query string parameters in the URL such as http://localhost:7000?date1=2013-07-22&date2=2013-07-23 . 您可以在URL中将日期参数指定为查询字符串参数,例如http://localhost:7000?date1=2013-07-22&date2=2013-07-23 To access these in express they will be req.query.date1 and req.query.date2 inside your app.get('/' handler function. 要快速访问它们,它们将是app.get('/'处理函数中的req.query.date1req.query.date2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM