0

I am new to nodejs. I am trying a basic example using http requests Get,Post and Put. I am done with POST and 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);

All I need is a PUT request on 'ADD SKILL' button in my response. I am not using 'Request' or 'Express' modules. Any suggestions how to go forward with PUT request ?

Niranth Reddy
  • 493
  • 1
  • 11
  • 27
  • Possible duplicate of [HTTP PUT Request with Node.js](http://stackoverflow.com/questions/7225045/http-put-request-with-node-js) – Samrat Das May 24 '16 at 06:56
  • I would like to do with using no modules. In the above solution, it used Request Module – Niranth Reddy May 24 '16 at 07:07
  • they are just using http module as yours, btw. how is it not working? getskill() is not getting called? – Samrat Das May 24 '16 at 07:12
  • Yes, to call getSkill() we need a call a Put request and my question is how to call a put request ? – Niranth Reddy May 24 '16 at 07:16
  • it is working, i tried doing `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);` and this code is working fine, put is getting called! – Samrat Das May 24 '16 at 07:23
  • If you just need to send a PUT to test, then something like [Postman](https://www.getpostman.com/) or even curl would work. – whostolemyhat May 24 '16 at 08:11

1 Answers1

0

May be this helps:

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.')
  })
});
AJS
  • 1,993
  • 16
  • 26