init
This commit is contained in:
parent
3337652b41
commit
81050bd297
|
|
@ -1 +1,4 @@
|
|||
MONGO_DSN="mongodb://localhost:27017"
|
||||
FROM node:22-alpine3.19
|
||||
|
||||
MONGO_DSN="mongodb://root:root@localhost:27017/crm"
|
||||
MONGO_DB="crm"
|
||||
34
app.js
34
app.js
|
|
@ -1,18 +1,32 @@
|
|||
require('dotenv').config()
|
||||
|
||||
const express=require('express')
|
||||
const fs=require('fs')
|
||||
const bodyParser=require('body-parser')
|
||||
const user=require("./handler/user")
|
||||
const multer=require('multer')
|
||||
const uploadConfig=multer({
|
||||
dest: './upload'
|
||||
const mongoose = require('mongoose')
|
||||
const morgan = require('morgan')
|
||||
const bodyParser = require('body-parser')
|
||||
|
||||
const EmployeeRoute=require('./routers/employee')
|
||||
|
||||
mongoose.connect(process.env.MONGO_DSN||'')
|
||||
const db = mongoose.connection
|
||||
|
||||
db.on('error',(err)=>{
|
||||
console.log(err)
|
||||
})
|
||||
|
||||
db.once('open',()=>{
|
||||
console.log('Database connection Established.')
|
||||
})
|
||||
|
||||
const app=express()
|
||||
app.use(express.json)
|
||||
app.use(bodyParser.urlencoded({extended:false}))
|
||||
|
||||
app.use(morgan('dev'))
|
||||
app.use(bodyParser.urlencoded({extended:true}))
|
||||
app.use(bodyParser.json())
|
||||
app.use(express.static("./public"));
|
||||
app.use("/user",user);
|
||||
|
||||
app.listen(8080,()=>{
|
||||
console.log('server is running on port: ',8080)
|
||||
});
|
||||
|
||||
app.listen(8080);
|
||||
app.use('/api/employee',EmployeeRoute)
|
||||
|
|
@ -1,8 +1,6 @@
|
|||
version: '3.5'
|
||||
|
||||
services:
|
||||
mongo:
|
||||
image: mongo:latest
|
||||
image: mongo:7.0.14
|
||||
ports:
|
||||
- 27017:27017
|
||||
environment:
|
||||
|
|
|
|||
|
|
@ -0,0 +1,94 @@
|
|||
const Employee = require('../model/employee')
|
||||
|
||||
const list=(req,res,next)=>{
|
||||
Employee.find()
|
||||
.then(response=>{
|
||||
res.json({
|
||||
response
|
||||
})
|
||||
})
|
||||
.catch(error=>{
|
||||
res.json({
|
||||
message: 'An error Occured!'
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const detail = (req,res,next)=>{
|
||||
let employeeID=req.body.employeeID
|
||||
Employee.findById(employeeID)
|
||||
.then(response=>{
|
||||
res.json({
|
||||
response
|
||||
})
|
||||
})
|
||||
.catch(err=>{
|
||||
res.json({
|
||||
message: 'An error Occured!'
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const create=(req,res,next)=>{
|
||||
let employee=new Employee({
|
||||
name:req.body.name,
|
||||
designation: req.body.designation,
|
||||
email:req.body.email,
|
||||
phone:req.body.phone,
|
||||
age: req.body.age
|
||||
})
|
||||
|
||||
employee.save()
|
||||
.then(response=>{
|
||||
res.json({
|
||||
response
|
||||
})
|
||||
})
|
||||
.catch(error=>{
|
||||
console.log(error)
|
||||
res.json({
|
||||
message: 'An error Occured!'
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const update=(req,res,next)=>{
|
||||
let employeeID=req.body.employeeID
|
||||
let updateData={
|
||||
name:req.body.name,
|
||||
designation: req.body.designation,
|
||||
email:req.body.email,
|
||||
phone:req.body.phone,
|
||||
age: req.body.age
|
||||
}
|
||||
Employee.findByIdAndUpdate(employeeID,{$set:updateData})
|
||||
.then(()=>{
|
||||
res.json({
|
||||
message: 'Employee updated successfully!'
|
||||
})
|
||||
})
|
||||
.catch(error=>{
|
||||
res.json({
|
||||
message: 'An error Occured!'
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const destroy=(req,res,next)=>{
|
||||
let employeeID=req.body.employeeID
|
||||
Employee.findByIdAndDelete(employeeID)
|
||||
.then(()=>{
|
||||
res.json({
|
||||
message: 'Employee deleted successfully.'
|
||||
})
|
||||
})
|
||||
.catch(error=>{
|
||||
res.json({
|
||||
message: 'An error Occred!'
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
module.exports={
|
||||
list,detail,create,update,destroy
|
||||
}
|
||||
|
|
@ -1,13 +0,0 @@
|
|||
const express=require("express");
|
||||
const router=express.Router();
|
||||
|
||||
router.get("detail",function(req,res){
|
||||
res.send("Hello World.")
|
||||
})
|
||||
|
||||
router.post("register",function(req,res){
|
||||
console.log(req.body)
|
||||
res.send("User created successfully.")
|
||||
})
|
||||
|
||||
module.exports=router;
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
const mongoose = require('mongoose')
|
||||
const Schema = mongoose.Schema
|
||||
|
||||
const employeeSchema = new Schema({
|
||||
name: {
|
||||
type: String
|
||||
},
|
||||
designation:{
|
||||
type: String
|
||||
},
|
||||
email:{
|
||||
type: String
|
||||
},
|
||||
phone: {
|
||||
type: String
|
||||
},
|
||||
age: {
|
||||
type: Number
|
||||
}
|
||||
},{timestamps: true})
|
||||
|
||||
const Employee = mongoose.model('Employee',employeeSchema)
|
||||
module.exports= Employee
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
const {MongoClient} = require('mongodb');
|
||||
|
||||
|
||||
require('dotenv').config()
|
||||
|
||||
function newDB(){
|
||||
const uri=process.env.MONGO_DSN
|
||||
|
||||
const client = new MongoClient(uri,{ useNewUrlParser: true, useUnifiedTopology: true })
|
||||
try {
|
||||
client.connect()
|
||||
const db=client.db(process.env.MONGO_DB)
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
const db = require("./mongo.js");
|
||||
|
||||
const mongo=db.newDB()
|
||||
|
|
@ -3,6 +3,9 @@
|
|||
"body-parser": "^1.20.3",
|
||||
"express": "^4.21.0",
|
||||
"mongo": "^0.1.0",
|
||||
"mongodb": "^6.9.0",
|
||||
"mongoose": "^8.6.2",
|
||||
"morgan": "^1.10.0",
|
||||
"multer": "1.4.5-lts.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
|
|||
128
pnpm-lock.yaml
128
pnpm-lock.yaml
|
|
@ -17,6 +17,15 @@ importers:
|
|||
mongo:
|
||||
specifier: ^0.1.0
|
||||
version: 0.1.0
|
||||
mongodb:
|
||||
specifier: ^6.9.0
|
||||
version: 6.9.0
|
||||
mongoose:
|
||||
specifier: ^8.6.2
|
||||
version: 8.6.2
|
||||
morgan:
|
||||
specifier: ^1.10.0
|
||||
version: 1.10.0
|
||||
multer:
|
||||
specifier: 1.4.5-lts.1
|
||||
version: 1.4.5-lts.1
|
||||
|
|
@ -56,6 +65,10 @@ packages:
|
|||
balanced-match@1.0.2:
|
||||
resolution: {integrity: sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==}
|
||||
|
||||
basic-auth@2.0.1:
|
||||
resolution: {integrity: sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg==}
|
||||
engines: {node: '>= 0.8'}
|
||||
|
||||
binary-extensions@2.3.0:
|
||||
resolution: {integrity: sha512-Ceh+7ox5qe7LJuLHoY0feh3pHuUDHAcRUeyL2VYghZwfpkNIy/+8Ocg0a3UuSoYzavmylwuLWQOf3hl0jjMMIw==}
|
||||
engines: {node: '>=8'}
|
||||
|
|
@ -273,6 +286,10 @@ packages:
|
|||
isarray@1.0.0:
|
||||
resolution: {integrity: sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==}
|
||||
|
||||
kareem@2.6.3:
|
||||
resolution: {integrity: sha512-C3iHfuGUXK2u8/ipq9LfjFfXFxAZMQJJq7vLS45r3D9Y2xQ/m4S8zaR4zMLFWh9AsNPXmcFfUDhTEO8UIC/V6Q==}
|
||||
engines: {node: '>=12.0.0'}
|
||||
|
||||
media-typer@0.3.0:
|
||||
resolution: {integrity: sha512-dq+qelQ9akHpcOl/gUVRTxVIOkAJ1wR3QAvb4RsVjS8oVoFjDGTc679wJYmUmknUF5HwMLOgb5O+a3KxfWapPQ==}
|
||||
engines: {node: '>= 0.6'}
|
||||
|
|
@ -317,6 +334,33 @@ packages:
|
|||
mongodb-connection-string-url@3.0.1:
|
||||
resolution: {integrity: sha512-XqMGwRX0Lgn05TDB4PyG2h2kKO/FfWJyCzYQbIhXUxz7ETt0I/FqHjUeqj37irJ+Dl1ZtU82uYyj14u2XsZKfg==}
|
||||
|
||||
mongodb@6.8.0:
|
||||
resolution: {integrity: sha512-HGQ9NWDle5WvwMnrvUxsFYPd3JEbqD3RgABHBQRuoCEND0qzhsd0iH5ypHsf1eJ+sXmvmyKpP+FLOKY8Il7jMw==}
|
||||
engines: {node: '>=16.20.1'}
|
||||
peerDependencies:
|
||||
'@aws-sdk/credential-providers': ^3.188.0
|
||||
'@mongodb-js/zstd': ^1.1.0
|
||||
gcp-metadata: ^5.2.0
|
||||
kerberos: ^2.0.1
|
||||
mongodb-client-encryption: '>=6.0.0 <7'
|
||||
snappy: ^7.2.2
|
||||
socks: ^2.7.1
|
||||
peerDependenciesMeta:
|
||||
'@aws-sdk/credential-providers':
|
||||
optional: true
|
||||
'@mongodb-js/zstd':
|
||||
optional: true
|
||||
gcp-metadata:
|
||||
optional: true
|
||||
kerberos:
|
||||
optional: true
|
||||
mongodb-client-encryption:
|
||||
optional: true
|
||||
snappy:
|
||||
optional: true
|
||||
socks:
|
||||
optional: true
|
||||
|
||||
mongodb@6.9.0:
|
||||
resolution: {integrity: sha512-UMopBVx1LmEUbW/QE0Hw18u583PEDVQmUmVzzBRH0o/xtE9DBRA5ZYLOjpLIa03i8FXjzvQECJcqoMvCXftTUA==}
|
||||
engines: {node: '>=16.20.1'}
|
||||
|
|
@ -344,6 +388,22 @@ packages:
|
|||
socks:
|
||||
optional: true
|
||||
|
||||
mongoose@8.6.2:
|
||||
resolution: {integrity: sha512-ErbDVvuUzUfyQpXvJ6sXznmZDICD8r6wIsa0VKjJtB6/LZncqwUn5Um040G1BaNo6L3Jz+xItLSwT0wZmSmUaQ==}
|
||||
engines: {node: '>=16.20.1'}
|
||||
|
||||
morgan@1.10.0:
|
||||
resolution: {integrity: sha512-AbegBVI4sh6El+1gNwvD5YIck7nSA36weD7xvIxG4in80j/UoK8AEGaWnnz8v1GxonMCltmlNs5ZKbGvl9b1XQ==}
|
||||
engines: {node: '>= 0.8.0'}
|
||||
|
||||
mpath@0.9.0:
|
||||
resolution: {integrity: sha512-ikJRQTk8hw5DEoFVxHG1Gn9T/xcjtdnOKIU1JTmGjZZlg9LST2mBLmcX3/ICIbgJydT2GOc15RnNy5mHmzfSew==}
|
||||
engines: {node: '>=4.0.0'}
|
||||
|
||||
mquery@5.0.0:
|
||||
resolution: {integrity: sha512-iQMncpmEK8R8ncT8HJGsGc9Dsp8xcgYMVSbs5jgnm1lFHTZqMJTUWTDx1LBO8+mK3tPNZWFLBghQEIOULSTHZg==}
|
||||
engines: {node: '>=14.0.0'}
|
||||
|
||||
ms@2.0.0:
|
||||
resolution: {integrity: sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==}
|
||||
|
||||
|
|
@ -375,10 +435,18 @@ packages:
|
|||
resolution: {integrity: sha512-IRZSRuzJiynemAXPYtPe5BoI/RESNYR7TYm50MC5Mqbd3Jmw5y790sErYw3V6SryFJD64b74qQQs9wn5Bg/k3g==}
|
||||
engines: {node: '>= 0.4'}
|
||||
|
||||
on-finished@2.3.0:
|
||||
resolution: {integrity: sha512-ikqdkGAAyf/X/gPhXGvfgAytDZtDbr+bkNUJ0N9h5MI/dmdgCs3l6hoHrcUv41sRKew3jIwrp4qQDXiK99Utww==}
|
||||
engines: {node: '>= 0.8'}
|
||||
|
||||
on-finished@2.4.1:
|
||||
resolution: {integrity: sha512-oVlzkg3ENAhCk2zdv7IJwd/QUD4z2RxRwpkcGY8psCVcCYZNq4wYnVWALHM+brtuJjePWiYF/ClmuDr8Ch5+kg==}
|
||||
engines: {node: '>= 0.8'}
|
||||
|
||||
on-headers@1.0.2:
|
||||
resolution: {integrity: sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA==}
|
||||
engines: {node: '>= 0.8'}
|
||||
|
||||
parseurl@1.3.3:
|
||||
resolution: {integrity: sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==}
|
||||
engines: {node: '>= 0.8'}
|
||||
|
|
@ -456,6 +524,9 @@ packages:
|
|||
resolution: {integrity: sha512-fDW/EZ6Q9RiO8eFG8Hj+7u/oW+XrPTIChwCOM2+th2A6OblDtYYIpve9m+KvI9Z4C9qSEXlaGR6bTEYHReuglA==}
|
||||
engines: {node: '>= 0.4'}
|
||||
|
||||
sift@17.1.3:
|
||||
resolution: {integrity: sha512-Rtlj66/b0ICeFzYTuNvX/EF1igRbbnGSvEyT79McoZa/DeGhMyC5pWKOEsZKnpkqtSeovd5FL/bjHWC3CIIvCQ==}
|
||||
|
||||
simple-update-notifier@2.0.0:
|
||||
resolution: {integrity: sha512-a2B9Y0KlNXl9u/vsW6sTIu9vGEpfKu2wRV6l1H3XEas/0gUIzGzBoP/IouTcUQbm9JWZLH3COxyn03TYlFax6w==}
|
||||
engines: {node: '>=10'}
|
||||
|
|
@ -559,6 +630,10 @@ snapshots:
|
|||
|
||||
balanced-match@1.0.2: {}
|
||||
|
||||
basic-auth@2.0.1:
|
||||
dependencies:
|
||||
safe-buffer: 5.1.2
|
||||
|
||||
binary-extensions@2.3.0: {}
|
||||
|
||||
body-parser@1.20.3:
|
||||
|
|
@ -799,6 +874,8 @@ snapshots:
|
|||
|
||||
isarray@1.0.0: {}
|
||||
|
||||
kareem@2.6.3: {}
|
||||
|
||||
media-typer@0.3.0: {}
|
||||
|
||||
memory-pager@1.5.0: {}
|
||||
|
|
@ -842,12 +919,55 @@ snapshots:
|
|||
'@types/whatwg-url': 11.0.5
|
||||
whatwg-url: 13.0.0
|
||||
|
||||
mongodb@6.8.0:
|
||||
dependencies:
|
||||
'@mongodb-js/saslprep': 1.1.9
|
||||
bson: 6.8.0
|
||||
mongodb-connection-string-url: 3.0.1
|
||||
|
||||
mongodb@6.9.0:
|
||||
dependencies:
|
||||
'@mongodb-js/saslprep': 1.1.9
|
||||
bson: 6.8.0
|
||||
mongodb-connection-string-url: 3.0.1
|
||||
|
||||
mongoose@8.6.2:
|
||||
dependencies:
|
||||
bson: 6.8.0
|
||||
kareem: 2.6.3
|
||||
mongodb: 6.8.0
|
||||
mpath: 0.9.0
|
||||
mquery: 5.0.0
|
||||
ms: 2.1.3
|
||||
sift: 17.1.3
|
||||
transitivePeerDependencies:
|
||||
- '@aws-sdk/credential-providers'
|
||||
- '@mongodb-js/zstd'
|
||||
- gcp-metadata
|
||||
- kerberos
|
||||
- mongodb-client-encryption
|
||||
- snappy
|
||||
- socks
|
||||
- supports-color
|
||||
|
||||
morgan@1.10.0:
|
||||
dependencies:
|
||||
basic-auth: 2.0.1
|
||||
debug: 2.6.9
|
||||
depd: 2.0.0
|
||||
on-finished: 2.3.0
|
||||
on-headers: 1.0.2
|
||||
transitivePeerDependencies:
|
||||
- supports-color
|
||||
|
||||
mpath@0.9.0: {}
|
||||
|
||||
mquery@5.0.0:
|
||||
dependencies:
|
||||
debug: 4.3.7(supports-color@5.5.0)
|
||||
transitivePeerDependencies:
|
||||
- supports-color
|
||||
|
||||
ms@2.0.0: {}
|
||||
|
||||
ms@2.1.3: {}
|
||||
|
|
@ -883,10 +1003,16 @@ snapshots:
|
|||
|
||||
object-inspect@1.13.2: {}
|
||||
|
||||
on-finished@2.3.0:
|
||||
dependencies:
|
||||
ee-first: 1.1.1
|
||||
|
||||
on-finished@2.4.1:
|
||||
dependencies:
|
||||
ee-first: 1.1.1
|
||||
|
||||
on-headers@1.0.2: {}
|
||||
|
||||
parseurl@1.3.3: {}
|
||||
|
||||
path-to-regexp@0.1.10: {}
|
||||
|
|
@ -984,6 +1110,8 @@ snapshots:
|
|||
get-intrinsic: 1.2.4
|
||||
object-inspect: 1.13.2
|
||||
|
||||
sift@17.1.3: {}
|
||||
|
||||
simple-update-notifier@2.0.0:
|
||||
dependencies:
|
||||
semver: 7.6.3
|
||||
|
|
|
|||
|
|
@ -0,0 +1,12 @@
|
|||
const express = require('express')
|
||||
const router=express.Router()
|
||||
|
||||
const EmployeeHandler=require('../handler/employee')
|
||||
|
||||
router.get('/list',EmployeeHandler.list)
|
||||
router.post('/create',EmployeeHandler.create)
|
||||
router.post('/update',EmployeeHandler.update)
|
||||
router.post('/detail',EmployeeHandler.detail)
|
||||
router.post('/delete',EmployeeHandler.destroy)
|
||||
|
||||
module.exports=router
|
||||
37
test.http
37
test.http
|
|
@ -1,13 +1,42 @@
|
|||
@url=http://localhost:8080
|
||||
@url=http://localhost:8080/api
|
||||
|
||||
###
|
||||
GET {{url}}/user/detail HTTP/1.1
|
||||
POST {{url}}/employee/detail HTTP/1.1
|
||||
|
||||
{
|
||||
"employeeID": "66e996d8c0f1d788626a2100"
|
||||
}
|
||||
|
||||
###
|
||||
POST {{url}}/user/register HTTP/1.1
|
||||
GET {{url}}/employee/list HTTP/1.1
|
||||
|
||||
###
|
||||
POST {{url}}/employee/create HTTP/1.1
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"username": "john_doe"
|
||||
"name": "john mike",
|
||||
"designation": "dsfsafdsafadsf",
|
||||
"email": "sdfs@sdf.com",
|
||||
"phone": "+8612363378941",
|
||||
"age": 25
|
||||
}
|
||||
|
||||
###
|
||||
POST {{url}}/employee/update HTTP/1.1
|
||||
|
||||
{
|
||||
"employeeID": "66e98e9249b0ceb1e0208b6e",
|
||||
"name": "kerry doe",
|
||||
"designation": "fremale",
|
||||
"email": "sdfs@s22df.com",
|
||||
"phone": "+8612365478941",
|
||||
"age": 20
|
||||
}
|
||||
|
||||
###
|
||||
POST {{url}}/employee/delete HTTP/1.1
|
||||
|
||||
{
|
||||
"employeeID": "66e996d8c0f1d788626a2100"
|
||||
}
|
||||
Loading…
Reference in New Issue