user7425922
user7425922

Reputation:

how to handle post data in client on express

I am using express.js to built the website. i use .ejs as my front end and nodejs for backend i want ejs to interact with the nodejs server. I use get request and i split the url and taking the data but its no where good so i want to interact ejs file to nodejs for eg when we are using php we will try to code as $_POST['somename'] and even we do dynamic programming in php by taking the data and embedding html and writing it. i want to know to handle post request in ejs file and store the post request data and handle it throughout the file

Upvotes: 1

Views: 1767

Answers (1)

Feras Allaou
Feras Allaou

Reputation: 36

As far as I understood you want to handle your form data, and in order to do so you have to use body-parser.

npm install body-parser

then in your app.js/server.js just add these lines

let bodyParser = require("body-parser")
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))

after that you will be able to get $_POST['name_here']

app.post("/whatever", (request, respone){
console.log(request.body.name_here) //same as $_POST['name_here']
})

Upvotes: 2

Related Questions