How to receive HTML information in Node js

Asked

Viewed 192 times

-1

Hello I would like to know how I can receive the data of my form in HTML in Node js, I have the login form and I want to receive and check if this user is valid, but I want to check in my file that check this in my file connection.js that will be done in Node, and if it is valid redirect it to index, I’m already a few days in this, I tried with:

req.body.username

But it doesn’t work, so I would like to know how I can send the data of this form to my file js to handle this information and return to the client, I want to do this using Ajax, I know it is not the best option but I think it is easier for me.

  • how is your HTML form? used method POST? Voce uses the express on your Node and if so, Voce has set up express.urlencoded()? You have yet to show more of your code, so we can help your problem. Try to edit your question.

  • Yes, I got it with req.body.username, I configured it right, and I used post method in route and HTML, but I can’t use it in connection.js file, I want to use this file just to check if the user exists, I don’t want to have to create a route for it.

  • and what appears in req.body.username. If you are using AJAX, that means Voce receives a JSON. configured the express.json()?

  • I didn’t use Ajax, I need to use it I think, maybe it’s better, and what returns from req.body.username is the name of the user that is filled in the input.

1 answer

0

You’d have to use the middleware - body-parser

If you’re using the express would look like this.

const express = require('express')
const app = express();
var bodyParser = require('body-parser')

var urlencodedParser = bodyParser.urlencoded({ extended: false })

app.use(bodyParser.urlencoded({ extended: false }))

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

     var  username = req.body.username
     var  pass     = req.body.password

});
  • Could I do it without creating a route?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.