-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
63 lines (51 loc) · 1.84 KB
/
server.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import express from 'express'
import 'dotenv/config'
import cors from 'cors'
import formidable from 'formidable'
import { MongoClient } from 'mongodb'
import cookieParser from 'cookie-parser'
import { recipeRouter } from './src/routes/recipeRoutes'
import { ingredientRouter } from './src/routes/ingredientsRoutes'
import { userRouter } from './src/routes/userRoutes'
import { queryTags } from './src/queryTags'
import { verifyNextAuthToken } from './src/middleware/verifyNextAuthToken'
const PORT = process.env.PORT || 5001
const app = express()
app.set('trust proxy', 1)
//app.use(cors({ credentials: true, origin: 'https://recipe-1.vercel.app' }))
app.use(cors({ credentials: true, origin: true }))
//app.use(express.json())
app.use(express.json({ limit: '25mb' }))
app.use(express.urlencoded({ limit: '25mb', extended: true }))
////////////
app.use(cookieParser())
app.use(verifyNextAuthToken)
MongoClient.connect(process.env.DB_URL as string, function (err, client) {
if (err) throw new Error('Cannot connect to MongoDB')
app.locals.db = client?.db('recipe')
if (!app.locals.db) throw new Error('Database is undefined')
app.use('/api/recipes', recipeRouter)
app.use('/api/ingredients', ingredientRouter)
app.use('/api/users', userRouter)
// currently: get all possible tags
app.get('/api/tags', async (req, res) => {
let dbRes = await queryTags(req)
res.json(dbRes)
})
// image submit endpoint
app.post('/api/imagesubmit', async (req, res) => {
const form = new formidable.IncomingForm({
uploadDir: './public/img',
keepExtensions: true,
})
form.parse(req, (err, fields, files) => {
// console.log(err, fields, files)
let uploadFilePath = files.photo.path.replace('public/', '')
res.json({ url: uploadFilePath })
})
})
///////////////
app.listen(PORT, () => {
console.log(`The application is listening on port ${PORT}!`)
})
})