【问题标题】:Put Request using pure NodeJs使用纯 NodeJs 提出请求
【发布时间】:2016-05-24 06:47:15
【问题描述】:

我是 nodejs 的新手。我正在尝试使用 http 请求 Get、Post 和 Put 的基本示例。我已经完成了 POST 和 GET。

var http = require("http");
var port = 8081;

function getLogin(req, resp){
    resp.writeHead(200, {"Content-Type" : "text/html" });
    resp.write("<html><body><form action='http://localhost:8081/home' method='post'><table><tr><td>Username : <input type='text' name='username' id='username' required/></td></tr><tr><td>Password  : <input type='password' name='password' id='password' required/></td></tr><tr><td><input type='submit' value='Login' /></td></tr></table></form></body></html>");
resp.end();
}

function getHome(req, resp){
     resp.writeHead(200 , {'Content-Type':'text/html'});
     resp.write("<html><body>Niranth<br><input type='button' value='Add Skill'/></body></html>");
     resp.end();
}

function getSkill(req, resp){

}

function get404(req, resp){
    resp.writeHead(404, "404", {"Content-Type" : "text/html" });
    resp.write("<html><body>404</body></html>");
    resp.end();
}


http.createServer(function(req, resp){
    if(req.method == 'GET'){
        if(req.url === "/"){
            console.log("hello get");
            getLogin(req, resp);
        }
        else
            get404(req, resp);
    }
    else if(req.method == 'POST'){  
        var data = '';
        if(req.url === "/home"){
            req.on('data', function(chunk) {
              data += chunk;
              console.log("hello post");
            });

            req.on('end', function() {
              // parse the data
              getHome(req, resp)
            });
        }
        else{
            console.log("error");
        }
    }    
    else if(req.method == 'PUT'){
         getSkill(req, resp);
    }

}).listen(port);

我只需要在回复中的“添加技能”按钮上提出 PUT 请求。 我没有使用“请求”或“快递”模块。 任何建议如何继续 PUT 请求?

【问题讨论】:

  • 我想不使用任何模块。在上面的解决方案中,它使用了请求模块
  • 他们只是使用 http 模块作为你的,顺便说一句。它怎么不工作?没有调用getskill()?
  • 是的,要调用 getSkill() 我们需要调用 Put 请求,我的问题是如何调用 put 请求?
  • 它工作正常,我尝试做var http = require("http"); var port = 8081; http.createServer(function(req, resp){ if(req.method == 'GET'){ console.log("hello get"); } else if(req.method == 'POST'){ console.log("hello post"); } else if(req.method == 'PUT'){ console.log("hello put"); } }).listen(port); 并且这段代码工作正常,put 被调用了!

标签: javascript node.js http put


【解决方案1】:

这可能会有所帮助:

var postData = {name:'someName'};
var options = {
  hostname: '<HOST_NAME>', // www.examplehost.com
  port: 80,
  path: '<PATH>', // /upload_something
  method: 'PUT',
  headers: {
    'Content-Type': '<CONTENT_TYPE>', // application/x-www-form-urlencoded
    'Content-Length': postData.length
  }
};

var req = http.request(options, (res) => {
  console.log(`STATUS: ${res.statusCode}`);
  console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
  res.setEncoding('utf8');
  res.on('data', (chunk) => {
    console.log(`BODY: ${chunk}`);
  });
  res.on('end', () => {
    console.log('No more data in response.')
  })
});

【讨论】:

    猜你喜欢
    • 2020-08-21
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 2018-05-04
    • 2013-11-01
    • 1970-01-01
    • 2012-10-20
    • 2016-10-21
    相关资源
    最近更新 更多