Defining a Route and Query in a Database
The route functions (get, post, put, delete) receives a callback function. That callback function receives two parameters (request and response) when someone access the route parameter. Request holds the information about incoming request while response is used to send a response back to the client. Each route combined with the port addess serves as an endpoint through which, the frontend can communicate to the backend server, send request and receive response. Provided below is the syntax through which we can create a route to submit the form data received from the frontend.
Node
const { username, password } = req.body;
const newForm = new Form({
username,
password
});
newForm.save((err, savedForm) => {
if (err) {
console.error(âError saving form:â, err);
res.status(500).json({ error: âError saving formâ });
} else {
console.log(âForm saved successfully:â, savedForm);
res.status(200).json({ message: âForm saved successfullyâ });
}
});
});
Getting Started with MERN Stack
M (MongoDB) E (Express.js) R (React.js) N (Node.js) stack is a popular Javascript language-based stack for building full-stack applications, MongoDB, ExpressJs, and NodeJs being responsible for server building and React for client-side development. This article is going to be your guide if you want to get started with the MERN stack development.
Table of Content
- 1. Setting Up Node.Js Environment
- 2. Starting a Node.Js Project and Installing Dependencies
- 3. Create a basic server with Express.Js
- 4. Connect to MongoDB
- 5. Define a Schema
- 6. Defining a Route and Query in a Database
- 7. Create a React App
- 8. Create a Component to Make a Backend Request
- 9. Combining Everything to Make a MERN Application
Contact Us