-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathserver.ts
More file actions
68 lines (55 loc) · 1.72 KB
/
Copy pathserver.ts
File metadata and controls
68 lines (55 loc) · 1.72 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
import express from 'express'
import cors from 'cors'
import { MongoClient } from 'mongodb'
const app = express()
app.use(cors())
app.use(express.json())
const url = 'mongodb+srv://arnabacharya1612_db_use:test123@ac-wzo6xjz.8peoruq.mongodb.net/?retryWrites=true&w=majority'
const client = new MongoClient(url)
async function start() {
await client.connect()
console.log('connected to mongodb')
const db = client.db('vinotes')
// register
app.post('/register', async (req, res) => {
const n = req.body.name
const e = req.body.email
const p = req.body.password
const found = await db.collection('users').find({ email: e }).toArray()
if (found.length > 0) {
res.json({ msg: 'user already exists' })
return
}
await db.collection('users').insertOne({ name: n, email: e, password: p })
console.log('new user registered')
res.json({ msg: 'registered ok' })
})
// login
app.post('/login', async (req, res) => {
const e = req.body.email
const p = req.body.password
const found = await db.collection('users').find({ email: e, password: p }).toArray()
if (found.length == 0) {
res.json({ msg: 'wrong email or password' })
return
}
console.log('user logged in')
res.json({ msg: 'login ok', userId: found[0]._id })
})
// save note
app.post('/save', async (req, res) => {
try {
const t = req.body.text
const u = req.body.userId
await db.collection('notes').insertOne({ text: t, userId: u, savedAt: new Date() })
res.json({ msg: 'saved' })
} catch (err) {
console.log(err)
res.status(500).json({ msg: 'error' })
}
})
app.listen(3000, () => {
console.log('server running on port 3000')
})
}
start()