Rahul Desai
Rahul Desai

Reputation: 15501

unable to send data from HTML form to Node server

I have setup a simple HTML form as below:

<!doctype html>
<html>
    <head>
        <title>Testing Echo server</title>
    </head>
    <body>
        <form method="post" action="/">
            <input type="text" id="keyword" name="keyword" />
            <input type="submit" value="echo" id="submit" />
        </form>
    </body>
</html>

My app.js looks like this:

var express = require('express');
var app = express();

app.get('/', function (req, res) {
    res.sendFile(__dirname + '/index.html');
});

app.post('/', function (req, res) {

        console.log(req.params);  // logged as {}
        res.writeHead(200);
        //req.pipe(res);       // throws error
        res.write('abc');      // works
        res.end();

});

app.listen(8080);

I am unable to access the parameters send from the form.

How do I fix this?

Upvotes: 0

Views: 70

Answers (1)

David Losert
David Losert

Reputation: 4802

First of all, you would need the body-parser middleware if you dont want to pull the data out of the http headers yourself.

Then you dont need to access the params as you dont have any, but the body of the request with req.body.

Upvotes: 1

Related Questions