server.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use strict';
  2. const Express = require('express');
  3. const Mongoose = require('mongoose');
  4. const MethodOverride = require('method-override');
  5. const BodyParser = require('body-parser');
  6. const _ = require('lodash');
  7. const crawlerRouter = require('./lib');
  8. const app = Express();
  9. const port = process.env.PORT || 3000;
  10. app.use(Express.static(__dirname+'/client'));
  11. // Add middleware nessary for Rest Api's
  12. app.use(BodyParser.urlencoded({extended:true}));
  13. app.use(BodyParser.json());
  14. app.use(MethodOverride('x-HTTP-method-Override'));
  15. // CORS Support
  16. app.use(function(req, res, next) {
  17. res.header('Access-Control-Allow-Origin', '*');
  18. res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
  19. res.header('Access-Control-Allow-Headers', 'Content-Type');
  20. next();
  21. });
  22. app.use('/crawler/', crawlerRouter);
  23. // Connect to MongoDB
  24. // Mongoose.connect('mongodb://localhost/appcrawler');
  25. Mongoose.connect('mongodb://root:trust4appdbuser@ds038547.mlab.com:38547/trust4app');
  26. Mongoose.connection.once('open', function () {
  27. // Load the models.
  28. app.models = require('./models');
  29. // Load the routes.
  30. var dbRoutes = require('./controllers');
  31. _.each(dbRoutes, function (controller, route) {
  32. app.use(route, controller(app, route));
  33. });
  34. app.listen(port, function () {
  35. console.log('Server started on port', port);
  36. });
  37. });