How is an HTTP POST request made in node.js?

后端 未结 21 2394
南方客
南方客 2020-11-21 23:54

How can I make an outbound HTTP POST request, with data, in node.js?

21条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 00:35

    I found a video which explains on how to achieve this: https://www.youtube.com/watch?v=nuw48-u3Yrg

    It uses default "http" module together with "querystring" and "stringbuilder" modules. The application takes two numbers (using two textboxes) from a web page and upon submit, returns sum of those two (along with persisting the values in the textboxes). This is the best example I could find anywhere else.

    var http = require("http");
    var qs = require("querystring");
    var StringBuilder = require("stringbuilder");
    
    var port = 9000;
    
    function getCalcHtml(req, resp, data) {
        var sb = new StringBuilder({ newline: "\r\n" });
        sb.appendLine("");
        sb.appendLine(" ");
        sb.appendLine("     
    "); sb.appendLine(" "); sb.appendLine(" "); sb.appendLine(" "); if (data && data.txtFirstNo) { sb.appendLine(" ", data.txtFirstNo); } else { sb.appendLine(" "); } sb.appendLine(" "); sb.appendLine(" "); sb.appendLine(" "); if (data && data.txtSecondNo) { sb.appendLine(" ", data.txtSecondNo); } else { sb.appendLine(" "); } sb.appendLine(" "); sb.appendLine(" "); sb.appendLine(" "); sb.appendLine(" "); if (data && data.txtFirstNo && data.txtSecondNo) { var sum = parseInt(data.txtFirstNo) + parseInt(data.txtSecondNo); sb.appendLine(" "); sb.appendLine(" ", sum); sb.appendLine(" "); } sb.appendLine("
    Enter First No:
    Enter Second No:
    Sum: {0}
    "); sb.appendLine("
    ") sb.appendLine(" "); sb.appendLine(""); sb.build(function (err, result) { resp.write(result); resp.end(); }); } function getCalcForm(req, resp, data) { resp.writeHead(200, { "Content-Type": "text/html" }); getCalcHtml(req, resp, data); } function getHome(req, resp) { resp.writeHead(200, { "Content-Type": "text/html" }); resp.write("HomeWant to some calculation? Click here"); resp.end(); } function get404(req, resp) { resp.writeHead(404, "Resource Not Found", { "Content-Type": "text/html" }); resp.write("404404: Resource not found. Go to Home"); resp.end(); } function get405(req, resp) { resp.writeHead(405, "Method not supported", { "Content-Type": "text/html" }); resp.write("405405: Method not supported"); resp.end(); } http.createServer(function (req, resp) { switch (req.method) { case "GET": if (req.url === "/") { getHome(req, resp); } else if (req.url === "/calc") { getCalcForm(req, resp); } else { get404(req, resp); } break; case "POST": if (req.url === "/calc") { var reqBody = ''; req.on('data', function (data) { reqBody += data; if (reqBody.length > 1e7) { //10MB resp.writeHead(413, 'Request Entity Too Large', { 'Content-Type': 'text/html' }); resp.end('413413: Request Entity Too Large'); } }); req.on('end', function () { var formData = qs.parse(reqBody); getCalcForm(req, resp, formData); }); } else { get404(req, resp); } break; default: get405(req, resp); break; } }).listen(port);

提交回复
热议问题