繁体   English   中英

使用Node.js发布表单数据

[英]Post form data with Node.js

如何将输入表单数据发布为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!')
    })
})

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

您要发出请求还是发出请求? 如果您尝试发出获取请求,则它应以这种方式工作

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);

});

暂无
暂无

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

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