Create a REST API with Node js & Express
app.js:-
const express=require("express");
const http=require("http");
require("./db/connection");
const router=require("./api/emp/index")
const employee=require("./api/emp/model");
const app=express();
const server=http.createServer(app);
const port=process.env.PORT || 3000;
const ip=process.env.IP || "localhost";
app.use(express.json())
app.use(router)
server.listen(port, ip, ()=>{
console.log("Express Server Listening on http://%s:%d", ip, port);
})
---------------------------------
connection.js:
const mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1:27017/EMPDB').then(()=>{
console.log("MongoDb Connection Sucessfull....");
}).catch((e)=>{
console.log("Mongodb connection Failed!!!");
})
------------------------------
Model.js:
const mongoose=require("mongoose");
const empSchema=new mongoose.Schema({
empid:{type:Number, maxlength:2, unique:true, required:true},
name:{type:String, maxlength:128, required:true},
gender:{type:String, maxlength:6, required:true},
mobile:{type:Number, maxlength:11, unique:true, required:true},
email:{type:String, maxlength:128, unique:true, match:/^\S+@\S+\.\S+$/, required:true},
doj:{type:String, maxlength:20, match:/^[0-9]{2}[\/][0-9]{2}[\/][0-9]{4}$/, required:true},
})
const employee=new mongoose.model("EmpCollection", empSchema);
module.exports=employee;
----------------------------
index.js:
const express=require("express");
const employee = require("./model");
const router=express.Router();
const {createEmpData, readEmpData, getPerticularEmpData, updateEmpData, deleteEmpData}=require("./controller")
router.post("/apiemployee", createEmpData)
router.get("/apiemployee", readEmpData)
router.get("/apiemployee/:id", getPerticularEmpData)
router.patch("/apiemployee/:id", updateEmpData)
router.delete("/apiemployee/:id", deleteEmpData)
module.exports=router
--------------------------------
controller.js:
const employee=require("./model")
// exports.createEmpData=async(req, res)=>{
// console.log("Create Employee Data");
// try {
// const {empid, name, gender, mobile, email, doj}=req.body
// const emp=new employee({
// empid:empid,
// name:name,
// gender:gender,
// mobile:mobile,
// email:email,
// doj:doj
// })
// const createEmp= await emp.save();
// res.send(createEmp)
// } catch (e) {
// res.send(e)
// }
// }
exports.createEmpData=async(req, res)=>{
console.log("Create Employee Data Another Way");
try {
const emp=new employee(req.body)
const createEmp= await emp.save();
res.status(201).send(createEmp)
} catch (e) {
res.status(500).send(e)
}
}
exports.readEmpData=async(req, res)=>{
console.log("Read Employee Data");
try {
const emp=await employee.find();
res.status(200).send(emp);
} catch (e) {
res.status(500).send(e);
}
}
exports.getPerticularEmpData=async(req, res)=>{
console.log("Get Perticular Employee Data");
try {
const _id=req.params.id;
const emp=await employee.findById(_id);
res.status(200).send(emp);
} catch (e) {
res.status(500).send(e);
}
}
exports.updateEmpData=async(req, res)=>{
console.log("Update Employee Data");
try {
const _id=req.params.id;
const emp=await employee.findByIdAndUpdate(_id, req.body, {new:true});
res.status(200).send(emp);
} catch (e) {
res.status(500).send(e);
}
}
exports.deleteEmpData=async(req, res)=>{
console.log("Delete Employee Data");
try {
const _id=req.params.id;
const emp=await employee.findByIdAndDelete(_id);
res.status(200).send(emp);
} catch (e) {
res.status(500).send(e);
}
}
Comments
Post a Comment