1

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

1 Answer 1

2

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']
})
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.