server.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import * as debugStuff from 'debug'
  2. const debug = debugStuff.debug('dbApiServer')
  3. debug('Starting the dbApiServer')
  4. const express = require('express')
  5. debug(' - Loaded express')
  6. const bodyParser = require('body-parser')
  7. debug(' - Loaded bodyParser')
  8. const http = require('http')
  9. debug(' - Loaded http')
  10. // const oauthserver = require('oauth2-server')
  11. const mongoose = require('mongoose')
  12. debug(' - Loaded mongoose')
  13. /** Create the express app */
  14. const app = express()
  15. debug('Created app')
  16. /** Bind the http server to express */
  17. const server = http.createServer(app)
  18. debug('Created server')
  19. /** MongoDB middleware */
  20. const dbName = 'AutoMateDB'
  21. const connectionString = `mongodb://localhost:27017/${dbName}`
  22. /** Connect the middleware to the server */
  23. mongoose.connect(connectionString)
  24. debug('Connected DB')
  25. function welcomeRouter (req, res) {
  26. res.status(200)
  27. res.json({ message: 'Welcome to the AutoMate DB API!' })
  28. }
  29. function errorRouter (req, res) {
  30. res.status(404)
  31. res.send()
  32. }
  33. debug('Defined default routers')
  34. app.use(bodyParser.json())
  35. app.use(bodyParser.urlencoded({ extended: true }))
  36. /** Load the submodules */
  37. // import projects from './projects/route'
  38. import routeGenModel from './routeGenModel'
  39. app.get('/', welcomeRouter)
  40. // app.use('/projects', projects)
  41. app.use(/.*/, errorRouter)
  42. debug('Configured app')
  43. const port = process.env.PORT || 4100
  44. server.listen(port)
  45. debug('Started server on port %s', port)