0

I have a problem with sending data on the server. I run server.js in node but always after clicking submit button req.body.username and req.body.password is undefinied... HTML file is opened in a browser, so probably it is the problem, but I have no idea how to solve it. I'm a beginner so be understanding...

index.html:

<form method="post" action="http://127.0.0.1:3000/">   
<fieldset>
<label for="username"></label>
<input type="text" id="username" placeholder="username">
<label for="password"></label>

<input type="password" id="password" placeholder="password">

<input type="submit" value="Create user">
</fieldset>
</form>

server.js:

 const express = require('express');
 const mysql = require('mysql');
 const bodyParser = require('body-parser');
 const http = require('http');

 const template = require('./template'); 
 const app = express();

 app.use(bodyParser.urlencoded({extended:true}));
 app.use(express.static('public'))
 app.use(bodyParser());

 app.post('/',(req, res) => {



  template.createUser( 

    req.body.username,
    req.body.password
  )

 });

 app.listen(3000, 'localhost' );

1 Answer 1

1

Add name attributes to your form:

<form method="post" action="http://127.0.0.1:3000/">
    <fieldset>
        <label for="username"></label>
        <input type="text" name="username" id="username" placeholder="username">
        <label for="password"></label>
        <input type="password" name="password" id="password" placeholder="password">
        <input type="submit" value="Create user">
    </fieldset>
</form>

See this answer for more info: HTML input - name vs. id

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.