简体   繁体   中英

Post form data with Node.js

How can i post input form data as a url. The sample code shown below.

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!')
    })
})

So I want get username on index page and post form value to the scrape.

Do you want to make a get or a post request? If you are trying to make a get request it should work this way

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

For form-data read more 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);

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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