NodeJs
Post Requests
When you build a website or an API with Node.js and Express, your server listens for different types of requests that come from the browser. The two most common request types are GET and POST.
A GET request is used when the browser wants to get or retrieve something from the server. This happens any time you visit a web page. For example going to http://localhost:8080/ your browser sends a GET request asking for the homepage.
In Node.js with Express, you handle this with app.get():
app.get("/", (req, res) => { /* some code in here */ });A POST request is used when you want to send information to the server. This usually happens when a user fills out and submits a form. For example when someone enters their name, email, and message and presses “Submit,” the browser sends a POST request with that data.
In Node.js with Express, you handle it with app.post():
app.post("/contact", (req, res) => { /* some code in here*/ });So while GET requests are used to retrieve data, POST requests are used to send new data to the server.
In a Node.js server, you can have both a GET and a POST route with the same path, like /contact. Even though the path is the same, they are treated as two separate routes because each one handles a different type of request. For example, the GET route might show the contact form, and the POST route might handle the form submission. You would write it like this:
Keep reading this lesson
You've reached the end of the free preview. Unlock the rest of this lesson plus 8 more lessons in NodeJs.
- Unlimited coding questions
- Unlimited access to all classroom material
- Weekly live call for questions and guidance
- 3,000 AI tutor tokens / month
- Personal SQL database (3 MB)
- Progress tracking
Cancel any time.