-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
43 lines (35 loc) · 1.17 KB
/
server.js
File metadata and controls
43 lines (35 loc) · 1.17 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
const express = require('express')
const app = express()
const path = require('path') //native module, no need to install
require('dotenv').config()
const logger = require('morgan')
const mongoose = require('mongoose')
const pe = require('parse-error')
const routes = require('./routes')
const port = 3000
//mongoose options
const mongooseOptions = {
useUnifiedTopology: true,
useNewUrlParser: true,
useCreateIndex: true
}
//connect to database
mongoose.connect(process.env.MONGO_URL, mongooseOptions)
.then(
() => console.log('Database Connection established!'),
err => console.log(err)
)
app.use(logger('dev')) // For logging out errors to the console
app.use(express.json()) // for parsing application/json
app.use(express.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded
app.use(express.static(path.join(__dirname, 'public'))) // For serving static files
//use routes
app.use('/api', routes)
//handle unhandled error
process.on('unhandledRejection', error => {
console.error('Uncaught Error', pe(error))
return
})
app.listen(port, () => {
console.log(`Server Stated on http://localhost:${port}`)
})