Home > Mobile >  Why does my data gets saved only when i use app.get?
Why does my data gets saved only when i use app.get?

Time:08-17

I have this node JS function to save a user on the Mongodb database

app.post("/user", async(req, res)=>{
const user = new User ({id: '8451845', name: "Jerry", lastname: "Tom", email: 
"[email protected]", pass: "123456789"});
try {
    await user.save();
} catch (err) {
    console.log(err);
}
})

And when i navigate to "http://localhost:3001/user" it tells me cannot Get /user, but when i use app.get instead of app.post the user gets saved perfectly on the database, so why does the app.get save the data, i thought the POST method is the one responsible for creating

CodePudding user response:

You might want to lookup how HTTP works. When visiting a page in a browser, the browser sends a GET request to the webserver. POST, PUT etc requests are made in the code. app.post allows the webserver to listen to a POST request.

  • Related