0
私はnodejsを初めて使用しています。私はHTTPリクエストGet、Post、Putを使って基本的な例を試しています。私はPOSTとGETで終わっています。純粋なNodeJを使用してリクエストを送信
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リクエストが必要です。 私は 'Request'または 'Express'モジュールを使用していません。 PUTリクエストを進めるにはどうすればよいですか?
[Node.jsのでHTTP PUT要求](の可能性のある重複http://stackoverflow.com/questions/7225045/http-put- request-with-node-js) –
モジュールを使用しないで使っています。上記のソリューションでは、リクエストモジュール –
を使用しています。これは、httpモジュールをあなたのものとして使用しています。どのように動作していないのですか? getskill()は呼び出されていませんか? –