【问题标题】:Post form data with Node.js使用 Node.js 发布表单数据
【发布时间】:2017-10-03 20:42:11
【问题描述】:

如何将输入表单数据作为 url 发布。示例代码如下所示。

app.get('/', (req, res) => {
    res.render('index');
});

app.post('/scrape', function(req, res){
    const name = req.body.username;
    res.send('You sent the name "' + name + '".');    
});

app.get('/scrape', function (req, res) {
    const name = req.body.username;
    res.send('You sent the name "' + name + '".');    

    url = `http://www.github.com/${name}`;

    request(url, function (error, response, html) {
        // 
        //
        //
        })
        res.send('Check your console!')
    })
})

所以我想在索引页面上获取用户名并将表单值发布到抓取。

【问题讨论】:

  • 仅供参考,对于同一个请求,您不能有两个 res.send() 语句。

标签: javascript node.js express web-scraping cheerio


【解决方案1】:

您要发出 get 或 post 请求吗? 如果您尝试发出获取请求,它应该以这种方式工作

request("http://www.github.com/whatever", function (error, response,html) {
console.log(response)
})

对于表单数据阅读更多https://github.com/request/request#forms

request.post({url:'http://service.com/upload', formData: formData}, 
function optionalCallback(err, httpResponse, body) {
  if (err) {
    return console.error('upload failed:', err);
  }
console.log('Upload successful!  Server responded with:', body);

});

【讨论】:

  • 对不起,误会了,其实我想使用提示索引页面的'name'数据并这样使用它; github.com{name}
猜你喜欢
  • 2017-06-05
  • 2018-10-11
  • 1970-01-01
  • 2015-11-28
  • 1970-01-01
  • 2011-09-09
  • 1970-01-01
  • 2013-01-20
  • 2020-12-02
相关资源
最近更新 更多