-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
60 lines (49 loc) · 1.29 KB
/
server.js
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
//const express = require('express')
// Lecture 14
/*
param middleware:
- a special paramter which runs on certain route paramter.
*/
const app = require('./Express/index.js');
const mongoose = require("mongoose");
const PORT = process.env.PORT || 3001;
mongoose.connect(process.env.MONGO_DB)
.then(() => {
console.log('Successfully connected to MongoDB!');
})
.catch((error) => {
console.error('Error connecting to MongoDB:', error.message);
});
// Example Schema and Model (Optional)
const UserSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
age: {
type: Number,
required: true
},
email: {
type: String,
required: true
},
});
const User = mongoose.model('User', UserSchema);
// Example Usage
async function addUser() {
const user = new User({ name: 'shetty', age: 21, email: 'shetty@gmail.com' });
await user.save();
console.log('User added:', user);
}
// update
async function retrive() {
User.find({name: "Zakir"}).then(users => console.log("All Users", users))
.catch(err => console.error("Error fetching users", err))
}
retrive();
// addUser();
// server
app.listen(PORT, ()=>{
console.log(`Express Server Started on PORT : ${PORT}`)
})