-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
43 lines (35 loc) · 1015 Bytes
/
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
require('dotenv').config()
const express = require('express')
const mongoose = require('mongoose')
const productRoute = require('./routes/route')
const app = express()
const errorMiddleware = require('./middleware/errorMiddleware')
var cors = require('cors')
const MONGO_URL = process.env.MONGO_URL
const PORT = process.env.PORT || 3000
const FRONTEND = process.env.FRONTEND || "http://localhost:5173/"
var corsOptions = {
origin: FRONTEND,
optionsSuccessStatus: 200
}
app.use(cors())
app.use(express.json())
app.use(express.urlencoded({extended: false}))
// routes
app.use('/api/product', productRoute);
app.get('/', (req, res) => {
res.send('Hello NODE API')
})
app.get('/about', (req, res) => {
res.send('Hello I am Anuraj Shukla')
})
app.use(errorMiddleware);
mongoose.connect(MONGO_URL)
.then(() => {
console.log('connected to MongoDB')
app.listen(PORT, ()=> {
console.log(`Node API app is running on port ${PORT}`)
})
}).catch((error) => {
console.log(error)
})