How to Create User Schema, Controllers, and Models in Node.js for a Sign-Up Process

How to Create User Schema, Controllers, and Models in Node.js for a Sign-Up Process

How to Create User Schema, Controllers, and Models in Node.js for a Sign-Up Process 🔑🚀💻🌐⚙️✏️

In this article, we will guide you through creating a user schema, controllers, and models in Node.js during the sign-up process. This tutorial will save users during registration using Postman for a hands-on experience.

Table of Contents

  1. Why create a User Schema?
  2. Define the structure of the user document
  3. User Schema Example
  4. User model and schema setup
  5. Controllers Setup
  6. Importing the model and creating user instances
  7. Saving Users at Sign-Up Process
  8. Saving users in MongoDB during registration
  9. Testing with Postman
  10. Verifying the saved user data in MongoDB

Why create a User Schema?

Before we get started, it's essential to understand why creating a user schema is crucial for your application. The user schema defines the structure of the user document, which includes fields such as name, email, and password. By setting up the user schema correctly, you ensure that your users have a seamless experience while registering on your platform.

User Schema Example

Let's dive into an example of creating a user schema in Node.js using Mongoose:
```javascript
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const UserSchema = new Schema({
name: { type: String, required: true },
email: { type: String, required: true, unique: true, lowercase: true },
password: { type: String, minlength: 6 }
});
nconst User = mongoose.model('User', UserSchema);``
In the example above, we define a User schema with fields for name, email, and password. We set theemail` field to be required, unique, and lowercase, ensuring that every user has a unique email address in lowercase.

Controllers Setup

Next, let's move on to setting up controllers in your application:
```javascript
nconst express = require('express');
nconst router = express.Router();
const User = require('./models/User');

router.post('/signup', async (req, res) => {
const { email, password } = req.body;

// Save the user to MongoDB
const user = await User.create({ email, password });
if (!user) return res.status(500).json({ error: 'Fail to create user' });

res.status(201).json({ user });
});
module.exports = router;``
In this example, we import the User model at the top of our file and create a new route for signing up users. We then extract the email and password from the request body, save the user to MongoDB usingUser.create`, and return the user data if successful.

Saving Users at Sign-Up Process

When a user submits the sign-up form, the controller function we created earlier is called to save the user in MongoDB:
```javascript
npm install mongoose --save

npm run dev
```Testing with Postman

To test our implementation, let's use Postman to send a POST request with the email and password fields filled in. If the user is saved successfully, we should receive a JSON object containing the user data, including an ID generated by MongoDB:
```yaml
POST /signup
{

Let’s talk about your project

Let's discuss your project and find the best solution for your business.

Optional

Max 500 characters