最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Send data to Node.js server from HTML form - Stack Overflow

programmeradmin1浏览0评论

I'm learning Node.js.

I have this in my server:

var http = require("http");
var url = require("url");

http.createServer(function(request, response){
    response.writeHead(200, {"Content-Type":"text/plain"});
    var params = url.parse(request.url,true).query;

    console.log(params);

    var a = params.number1;
    var b = params.number2;

    var numA = new Number(a);
    var numB = new Number(b);

    var numOutput = new Number(numA + numB).toFixed(0);

    response.write(numOutput);
    response.end();
}).listen(10000);

An URL like this: localhost:10000/?number1=50000&number2=1 echoes 50001 on my browser, so it works.

Without using Express, I need to send those 2 params through a form using HTML.

How can this be achieved?

I'm learning Node.js.

I have this in my server:

var http = require("http");
var url = require("url");

http.createServer(function(request, response){
    response.writeHead(200, {"Content-Type":"text/plain"});
    var params = url.parse(request.url,true).query;

    console.log(params);

    var a = params.number1;
    var b = params.number2;

    var numA = new Number(a);
    var numB = new Number(b);

    var numOutput = new Number(numA + numB).toFixed(0);

    response.write(numOutput);
    response.end();
}).listen(10000);

An URL like this: localhost:10000/?number1=50000&number2=1 echoes 50001 on my browser, so it works.

Without using Express, I need to send those 2 params through a form using HTML.

How can this be achieved?

Share Improve this question asked Oct 9, 2013 at 21:09 Oscar SwanrosOscar Swanros 20k5 gold badges34 silver badges48 bronze badges 3
  • 1 stackoverflow.com/questions/15568851/… – Andy897 Commented Oct 9, 2013 at 21:10
  • @Andy897 — already looked at that. However, my teacher doesn't want me to use Express. – Oscar Swanros Commented Oct 9, 2013 at 21:12
  • stackoverflow.com/questions/8811213/… – Andy897 Commented Oct 9, 2013 at 21:13
Add a comment  | 

3 Answers 3

Reset to default 5

The simple answer is <form method="get">. The browser will turn the form data into query string parameters that you're already handling.

If you need to POST, HTML forms are posted as the request entity-body. In node, a ClientRequest (the request variable in your example) emits a data event every time a chunk of the body arrives at the server. You will not receive the entire body at once. You must buffer until you've received the entire body, then parse the data.

This is hard to get right because of things like chunked vs normal Transfer-Encoding and the different ways browsers can submit form data. I'd just use formidable (which is what Express uses behind the scenes anyway), or at least study how it handles form posts if you absolutely must implement your own. (And really, do this only for educational purposes – I can't stress enough that you should use formidable for anything that might end up in production.)

Your jade form (assuming you're using express) should look like this:

form(action="/" method="post")
    input( name="whateverSearch" placeholder="search" autofocus)

Then to pass the newly submitted datapoint whateverSearch you use req.body to find it (see below). Here, I've taken the data point and logged it to the console, and then submitted it to the DOM, as well as the title for the page.

router.post('/', function(req, res) {
    whateverSearch = req.body.whateverSearch;
    console.log(whateverSearch);
    res.render('index', { title: 'MyApp', inputData: whateverSearch});
});

I solved my problem this way:

sum.js:

var http = require("http");
var url = require("url");

http.createServer(function(request, response){
    response.writeHead(200, {"Content-Type":"text/plain"});
    var params = url.parse(request.url,true).query;

    console.log(params);

    var a = params.number1;
    var b = params.number2;

    var numA = new Number(a);
    var numB = new Number(b);

    var sum = new Number(numA + numB).toFixed(0);

    response.write(sum);
    response.end();
}).listen(10001);

Index.html:

<html>
    <head>
            <title> Pedir random </title>

            <script type="text/javascript">
                function operacion(){
                        var n1 = document.getElementById("num1").value;
                        var n2 = document.getElementById("num2").value;

                        location.href = "http://localhost:10001" + "?number1=" + n1 + "&number2=" + n2;
                }
            </script>

    </head>

    <body>
            <h1>Inserte dos números </h1>
            <form id="forma1"> 
                <input type="text" id="num1"></input>
                <input type="text" id="num2"></imput>
                <input type="button" onClick="operacion()" id="enviar" value="enviar"></input>
            </form>
    </body>

<html>

Don't know if it's the right way of doing it, but it solved my needs.

发布评论

评论列表(0)

  1. 暂无评论