Express Node JS GET request from HTML

I am taking my first jab at Express Node JS and thoroughly enjoying it. I'm looking to understand how to get first name and last name information input by a user to the server and displaying it in the console log.

My code in HTML is as follows:

<form action = "/getname/" method = "GET"> 
    <p>First name: <input type="text" id="fname" name="firstname"></p> 
    <p>Last name: <input type="text" id="lname" name="lastname"></p>
    <p><input type="button" value="Submit" id="namebutton" /></p>
    <p><span id="JSONname"></span></p>
</form>

My code in Express app.js is:

app.get('/getname/', function (req, res) {
    response = {
        firstname: req.query.firstname,
        lastname: req.query.lastname
    };
    console.log(response);
    res.end(JSON.stringify(response));
})

I think the connection between the localhost:xxxx/getname and the server is working, however the html to getname isn't working.

Might anyone see where the code is going wrong?

Upvotes: 0

Views: 341

Answers (1)

SuperOP535
SuperOP535

Reputation: 229

The submit button must be type="submit" and not type="button".

Upvotes: 1

Related Questions