index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. const express = require('express')
  2. const bodyParser = require('body-parser')
  3. const http = require('http')
  4. // const oauthserver = require('oauth2-server')
  5. const mongoose = require('mongoose')
  6. /** Load the submodules */
  7. import project from './project/route'
  8. /** Create the express app */
  9. const app = express()
  10. /** MongoDB middleware */
  11. const dbName = 'AutoMateDB'
  12. const connectionString = `mongodb://localhost:27017/${dbName}`
  13. /** Bind the http server to express */
  14. const server = http.createServer(app)
  15. /** Connect the middleware to the server */
  16. mongoose.connect(connectionString)
  17. function welcomeRouter (req, res) {
  18. res.status(200)
  19. res.json({ message: 'Welcome to the AutoMate DB API!' })
  20. }
  21. function errorRouter (req, res) {
  22. res.status(404)
  23. res.send()
  24. }
  25. app.use(bodyParser.json())
  26. app.use(bodyParser.urlencoded({ extended: true }))
  27. /*
  28. app.oauth = oauthserver({
  29. model: {},
  30. grants: ['password'],
  31. debug: true
  32. })
  33. app.all('/oauth/token', app.oauth.grant())
  34. app.get('/', app.oauth.authorise(), (req, res) => {
  35. res.send('Secret area')
  36. })
  37. app.use(app.oauth.errorHandler())
  38. */
  39. app.get('/', welcomeRouter)
  40. app.use('/project', project)
  41. app.use(/.*/, errorRouter)
  42. server.listen(process.env.PORT || 4100)