forked from RunnableDemo/node-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (35 loc) · 1.1 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
const express = require('express')
const mongoose = require('mongoose')
mongoose.Promise = global.Promise
const bodyParser = require('body-parser')
const app = express()
const host = process.env.RUNNABLE_CONTAINER_URL || 'localhost'
const port = process.env.PORT || 3000
const mongoHost = process.env.MONGODB_HOST || 'localhost'
mongoose.connect(`mongodb://${mongoHost}/todo`)
mongoose.connection.on('error', () => {
console.log('ERROR: Unable to connect to MongoDB.')
})
const todos = require('./todos')
function logErrors (err, req, res, next) {
console.error(err.stack)
next(err)
}
function errorHandler (err, req, res, next) {
res.status(500)
res.json({ error: err })
}
app.listen(port, () => {
var hostName = 'http://' + host + ':' + port
console.log('Application running at: ' + hostName)
})
app.use(bodyParser.json())
app.use(logErrors)
app.use(errorHandler)
// Routes
app.use('/', express.static('public'))
app.get('/api/todos', todos.all)
app.get('/api/todos/:id', todos.one)
app.post('/api/todos', todos.create)
app.put('/api/todos/:id', todos.update)
app.delete('/api/todos/:id', todos.delete)