Build RESTful APIs with Node.js express and MySQL | Authentication with JWT

Build RESTful APIs with Node.js express and MySQL | Authentication with JWT

YouTube Link: https://www.youtube.com/watch?v=WfCJ3sHnLBM

app.js

require("dotenv").config();
const express = require("express");
const app = express();
const userRouter = require("./api/users/user.router")
app.use(express.json()); 
app.use("/api/users", userRouter);
app.listen(process.env.APP_PORT, () => {
    console.log("node server working", process.env.APP_PORT )
})

database config code in db.js

const {createPool} = require("mysql");
const pool = createPool({
    port : process.env.DB_PORT,
    host : process.env.DB_HOST,
    user : process.env.DB_USER,
    password : process.env.DB_PASS,
    database : process.env.MQSQL_DB,
    connectionLimit: 10
})

module.exports = pool;

Service.js

const pool  =  require("../../config/database");

module.exports = {
    create: (data, callBack) => {
        pool.query(
            `insert into registration(firstName, lastName, gender, email, password, number) values(?,?,?,?,?,?)`,
             [
                data.first_name,
                data.last_name,
                data.gender,
                data.email,
                data.password,
                data.number,
             ],
             (error, results, fields) => {
                if (error) { 
                    return callBack(error);
                }
                return callBack(null, results)
             }
        );
    }
}

Controller.js

const  { create } = require("./user.service");
const { genSaltSync, hashSync } = require("bcrypt");

module.exports = {
    createUser: (req, res) => {
        const body = req.body;
        const salt =  genSaltSync(10);
        body.password = hashSync(body.password, salt);
        create(body, (err, results) =>{
            if (err) { 
                console.log(err, body);
                return res.status(500).json({
                    success : 0,
                    message : "Database connection error"
                });
            }
            return res.status(200).json({
                success : 1,
                data : results
            })
        })
    }
}

Router.js

const { createUser } = require("./user.controller");
const router = require("express").Router();

router.post("/", createUser);

module.exports = router;

.js


.js


.js


.js


.js


.js