-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay17.js
38 lines (32 loc) · 848 Bytes
/
Day17.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
const express = require('express');
const mongoose = require('mongoose');
const app = express();
const PORT = 6000;
//connection
mongoose.connect("mongodb://127.0.0.1:27017/learn_node")
//Schema
const userSchema = new mongoose.Schema({
username:{
type:String,
require:true,
},
email:{
type:String,
require:true,
},
});
//models
const User = mongoose.model('User', userSchema);
async function addUserToDatabase(user) {
try {
const newUser = new User(user);
await newUser.save();
console.log('connection successful to Db');
} catch (error) {
console.error('database connection error:', error);
}
}
addUserToDatabase({ username: 'john_doe', email: 'john@example.com' });
app.listen(PORT, () => {
console.log(`Server is running at port: ${PORT}`);
});