Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backend: initial setup #25

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
const express = require("express");
const { router } = require('./routes/index');
const cors = require("cors");
const app = express();
app.use(express.json());

app.use(cors());

app.use('/api/v1', router);
app.listen(3001, () => {
console.log('Server is running on port 3001');
})

74 changes: 74 additions & 0 deletions backend/model/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const mongoose = require('mongoose');
const { Schema } = mongoose;
const uniqueValidator = require("mongoose-unique-validator");
const bcrypt = require('bcrypt');


const userSchema = new Schema({
mobileNumber: {
type: String,
required: true,
unique: true,
trim: true,
minlength: 10,
maxlength: 10,
},
email: {
type: String,
required: true,
unique: true,
lowercase: true,
trim: true,
match: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
},
password: {
type: String,
required: true,
minlength: 8
},
firstName: {
type: String,
required: true,
trim: true
},
lastName: {
type: String,
required: true,
trim: true
},
createdAt: {
type: Date,
default: Date.now(),
},
updatedAt: {
type: Date,
default: Date.now()
}
});

// Virtual field for full name
userSchema.virtual('fullName').get(function () {
return `${this.firstName} ${this.lastName}`
});

// This is a pre-defined middleware for userSchema.
// This middleware will be executed before save() operation is
// performed on any document using this schema.
userSchema.pre('save', async function (next) {
if (this.isModified('passoword')) {
const salt = await bcrypt.genSalt(10);
this.password = await bcrypt.hash(this.password, salt);
}
next();
})


//Plugin for unique validation

userSchema.plugin(uniqueValidator, { message: '{PATH} must be unique.' })


const User = mongoose.model('User', userSchema);

module.exports = User;

Loading