Adel Benyahia
1 min readDec 24, 2022

Thanks for sharing 👍

1. In my my opinion we can use the new node 18 “ — watch” flag to replace nodemon.scripts": {
"start": "node --watch index.js",

}

It works perfectly for me ☺️

2. in Sign up Logic

Sign up Logic

Check if the user exists or not, if the user already exists, throw errors with the message email already exists.

If the user is a new user, use bcrypt to hash the password before storing it in your database

Save data(name, email, and password) in MongoDB.

You have saved the hashed password not the password it self

Save data(name, email, and hashed password) in MongoDB.

3. A better way to implement cors package

  • create a new file: “config/allowedOrigins.js” that contain the list of allowed origins
const allowedOrigins = [
'http://localhost:3000',
'http://127.0.0.1:3000',
'https://www.yourwebsite.com', //front-end website
]
module.exports = allowedOrigins
  • create a new file: “config/corsConfigs.js” that contain the cors package configuration options
const allowedOrigins = require('./allowedOrigins')

// const port = process.env.PORT || 3500;
const corsConfigs = {
origin: (origin, callback) => {
if (allowedOrigins.indexOf(origin) !== -1 || !origin) {
// remove ||!origin to block postman request

callback(null, true)
} else {
callback(new Error('origin not allowed by Cors'))
}
},
// origin: [`http://localhost:${port}`, `https://localhost:${port}`],
credentials: true,
optionsSuccessStatus: 200,
}
module.exports = corsConfigs
  • Call the cors package with “corsConfigs” as parameters
const corsConfigs = require('./config/corsConfigs')

....

app.use(cors(corsConfigs))

More details

Adel Benyahia

Web application developer (HTML │ CSS │ JS | ReactJS | NextJS | NestJS | MERN)