forked from Code-Immersives/car-parts-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
41 lines (38 loc) · 1.17 KB
/
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
const express = require('express')
const app = express()
const port = process.env.PORT || 3000
const logger = require('morgan')
const bodyParser = require('body-parser')
// MONGODB SETUP
// brew install MONGODB
// mongod to start server
// sudo mkdir -p data/db
// sudo chown -R toneloke ./data/db
const mongoose = require('mongoose')
// establish a connection to the database
mongoose.connect('mongodb://localhost/cars', err => {
if (err) {
console.log('error connecting to mongodb:', err)
} else {
console.log('successfully connecting to mongodb cars db')
}
})
// using 3rd party middleware
app.use(logger('dev'))
// use the body-parser middleware to access req.body
// parse application/json
app.use(bodyParser.json())
// make routes availble to client
const carRoutes = require('./routes').carsRouter
const partRoutes = require('./routes').partsRouter
app.use('/api/v1', carRoutes)
app.use('/api/v1', partRoutes)
// run your server to listen on a given port
app.listen(port, (err) => {
// check for an error when communicating with the server
if (err) {
console.log('server failed to start', err)
} else {
console.log(`You're connected to port: ${port}`)
}
})