index.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. const express = require('express')
  2. const bodyParser = require('body-parser')
  3. const sockjs = require('sockjs')
  4. const http = require('http')
  5. const mongoose = require('mongoose')
  6. const router = express.Router()
  7. /** Load the submodules */
  8. import ProjectModel from './project/model'
  9. /** Create the express app */
  10. const app = express()
  11. /** MongoDB middleware */
  12. const dbName = 'movieDB'
  13. const connectionString = `mongodb://localhost:27017/${dbName}`
  14. /** Sockjs middleware */
  15. const sockjsOpts = {sockjs_url: 'http://cdn.jsdelivr.net/sockjs/1.0.1/sockjs.min.js'}
  16. const sockjsServer = sockjs.createServer(sockjsOpts)
  17. sockjsServer.on('connection', function (conn) {
  18. conn.on('data', function (message) {
  19. conn.write(message)
  20. })
  21. })
  22. /** Bind the http server to express */
  23. const server = http.createServer(app)
  24. function f1 (req, res, next) {
  25. console.log('f1', req)
  26. next()
  27. }
  28. function f2 (req, res, next) {
  29. console.log('f2')
  30. next()
  31. }
  32. function f3 (req, res, next) {
  33. console.log('f3')
  34. res.json({message: 'Welcome to the api!'})
  35. }
  36. sockjsServer.installHandlers(server, {prefix: '/echo'})
  37. mongoose.connect(connectionString)
  38. app.use(bodyParser.json())
  39. app.use(bodyParser.urlencoded({ extended: true }))
  40. app.use(/^\/db(\/\w+)?(\/\w+)?\/?$/, [f1, f2, f3])
  41. /* app.get('/', function (req, res) {
  42. res.sendFile(`${__dirname}/index.html`)
  43. }) */
  44. server.listen(process.env.PORT || 4000)