-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
118 lines (68 loc) · 2.31 KB
/
index.js
File metadata and controls
118 lines (68 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const express = require('express');
const app = express()
require('dotenv').config()
const cors = require('cors');
const { MongoClient, ServerApiVersion } = require('mongodb');
const port = 3000
app.get('/', (req, res) => {
res.send('ai inventor project server sunning ')
})
// middle wire here
app.use(cors());
app.use(express.json());
const uri = `mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASS}@cluster0.exfto5h.mongodb.net/?appName=Cluster0`;
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
});
async function run() {
try {
await client.connect();
// clreate collections here
const db = client.db('ai_model_inventory_manager');
const modelscollections = db.collection('models');
const userscollections = db.collection('users');
/* here the models get api: */
app.get('/models', async(req, res) => {
const cursor = modelscollections.find();
const result = await cursor.toArray(cursor);
res.send(result);
})
// add model here
app.post('/models', async(req,res) => {
const newmodel = req.body;
const result = await modelscollections.insertOne(newmodel);
res.send(result);
})
app.get('/recent-model', async(req,res) => {
const cursor = modelscollections.find().sort({createdAt: -1}).limit(6);
const result = await cursor.toArray();
res.send(result);
})
// detils page here:
app.get('/models/:id', async(req,res) => {
const id = req.params.id;
const query = {id: id};
const result = await modelscollections.findOne(query);
res.send(result);
})
/* ........ user related api here.......... */
app.post('/users', async(req, res) => {
const user = req.body;
const result = await userscollections.insertOne(user);
res.send(result);
})
await client.db("admin").command({ ping: 1 });
console.log("Pinged your deployment. You successfully connected to MongoDB!");
} finally {
}
}
run().catch(console.dir);
// mongo user: ai_model_inventory_manager
// mongo pass: AAir8C8na2P1uNm1
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})