No request from form in node js

安稳与你 提交于 2020-01-06 08:06:14

问题


I'm working with node js, express and handlebars with two forms. The first form is for doing the login and the second one for registering the new user. They are almost similar but the first one is working and the second one, no and I don't know why.

This is the code from register.hbr

        <div class="login-page">
  <div class="form">
    <form class="login-form" name="register" method="POST" action="/register_success">
      <input type="text" placeholder="email" required/>
      <input type="password" placeholder="password" required/>
      <input type="text" placeholder="nombre"/>
      <input type="submit" value="Crear cuenta" name="register_success"></input>
      <p class="message">Ya registrado? <a href="#">Entrar</a></p>
    </form>
  </div>
</div>

And this is the router that I've in node js

rutas.post('/register_success', (req, resp)=>{
console.log(req.body);
const email = req.body.email;
const nombre = req.body.name;
const password = req.body.password;

firebase.auth().createUserWithEmailAndPassword(email, password).then(user=>{
    resp.send('Usuario creado con exito');
}).catch(function(error) {
    // Handle Errors here.
    var errorCode = error.code;
    var errorMessage = error.message;
    resp.render('loginError');
  });    
});

My issue is that the request is always empty and IDK how to trace or look for the problem. Any help?


回答1:


In order to process form data in an Express endpoint, you will need a middleware that is able to extract this information. There are many available, but the simplest way to do is is to use body-parser. Add this to your main file where your app is defined:

const bodyParser = require("body-parser");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: true,
}));

If in the future you require processing more complex forms such as those with attachments, you will most likely need an external library such as multer.




回答2:


req.body is not populated by default. To get it populated, you need some code somewhere that reads the rest of the http request body (the data that comes after the headers) and then parses it according to its data type. A form submitted by the browser like yours will put the data in the body of the http request formatted as application/x-www-form-urlencoded data.

In Express, there is some built-in middleware for parsing that specific type of request. You will just need to enable it with one of these schemes:

app.use(express.urlencoded());

where this middleware is placed before your app.post() handler.

Or, you can enable the middleware only on the specific request handler that needs it:

rutas.post('/register_success', express.urlencoded(), (req, resp)=>{

FYI, Express also has body parsing middleware for JSON data which is often used for programmatic POSTs (such as POSTs from a Javascript AJAX call of programmatic POSTs to an API from any language). That middleware is not needed for a generic browser form submission like your HTML shows, but would be enabled for other purposes with:

app.use(express.json());



回答3:


Finally I found the issue. The proble is that I didn't name the inputs, and because of that req.body were empty. After renaming them like this, everything worked.

      <input type="text" name="email" placeholder="email" required/>
      <input type="password" name="password" placeholder="password" required/>
      <input type="text" name="nombre" placeholder="nombre"/>
      <input type="submit" value="Crear cuenta" name="register_success">  </input>


来源:https://stackoverflow.com/questions/59583577/no-request-from-form-in-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!