-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (40 loc) · 1.3 KB
/
index.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const express = require("express")
const mongoose = require("mongoose")
const cors = require("cors")
const productHandler = require('./routeHandler/productHandler')
const customerHandler = require('./routeHandler/customerHandler')
const orderHandler = require('./routeHandler/orderHandler')
const port = process.env.PORT || 5000
require('dotenv').config()
// express app initialization
const app = express()
// use middleware
app.use(express.json())
app.use(cors())
// database connection through mongoose
mongoose.connect(`mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASSWORD}@cluster0.wohsqtl.mongodb.net/?retryWrites=true&w=majority`, {
useUnifiedTopology: true,
useNewUrlParser: true,
autoIndex: true
})
.then(() => console.log("Connection successful"))
.catch(err => console.log(err))
// application routes declare
app.use('/product', productHandler)
app.use('/customer', customerHandler)
app.use('/order', orderHandler)
// error handler
function errorHandler(err, req, res, next) {
if(res.headerSent){
return next(err)
}
res.status(500).json({error: err})
}
//running
app.get('/', (req, res) => {
res.send('Hey, Welcome to Cosmetics Ecommerce Server API')
})
//listening port
app.listen(port, () => {
console.log(`Listening on port ${port}`);
})