-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
36 lines (29 loc) · 880 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
const express = require('express')
const bodyParser = require('body-parser')
const routes = require('./routes/api')
const mongoose = require('mongoose')
// Set up express app
const app = express()
// connect to mongodb
mongoose
.connect(
'mongodb://localhost:27017/spelling',
{ useNewUrlParser: true }
)
.then(console.log('mongo DB connected'))
.catch(err => console.log(err))
mongoose.Promise = global.Promise
// express
app.use( express.static( `${__dirname}/./build` ) );
// bodyParser: Parses the text as JSON and exposes the resulting object on req.body
app.use(bodyParser.json())
// initialise routes
app.use('/api', routes)
// error handling middleware
app.use((err, req, res, next) => {
res.status(422).send({ error: err.message })
})
// listen for requests
app.listen(process.env.port || 4000, () =>
console.log('now listening for requests')
)