-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
44 lines (35 loc) · 1.25 KB
/
index.ts
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
import express from 'express'
import cors from 'cors'
import { MongoClient, ObjectId } from 'mongodb'
import 'dotenv/config'
const app = express()
app.use(cors())
app.use(express.json())
const PORT = process.env.PORT
const client = new MongoClient(process.env.MONGO_URI as string)
const db = client.db('recipe-app')
const recipe = db.collection('recipes')
const users = db.collection('users')
app.listen(PORT , () => console.log('Database listening on port', PORT))
app.get('/', (req, res) => {
res.json('Here is my API responding')
})
//signup
app.post('/signup', async (req, res) => {
const { firstName, lastName, email, password } = req.body
const newUser = await users.insertOne( { firstName: firstName, lastName: lastName, email: email, password: password })
res.status(201).send(newUser)
})
//update recipe
app.patch('/:_id', async (req, res) => {
const cleanId = new ObjectId(req.params._id)
const updatedRecipe = await recipe.findOneAndUpdate( {_id: cleanId}, {$set: req.body})
res.send(updatedRecipe)
})
//delete recipe
app.delete('/:_id', async (req, res) => {
const cleanId = new Object (req.params._id)
console.log('req.params->', req.params)
const recipeDeleted = await recipe.findOneAndDelete({ _id: cleanId })
res.send(recipeDeleted)
})