Guest User

Untitled

a guest
Jun 7th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. import express from 'express';
  2. import bodyParser from 'body-parser';
  3. import Sequelize from 'sequelize';
  4. import morgan from 'morgan';
  5.  
  6. import {
  7. getPlacesIndex,
  8. getWishlistIndex,
  9. postWishlistPlaces,
  10. deleteWishlistPlaces,
  11. } from './routes';
  12. import initModels from './models';
  13. import {
  14. seeds,
  15. plant,
  16. } from './seeds';
  17.  
  18. const sequelize = new Sequelize({
  19. host: '127.0.01',
  20. dialect: 'mysql',
  21. username: 'root',
  22. password: null,
  23. database: 'doumi_dev',
  24. pool: {
  25. max: 5,
  26. min: 0,
  27. acquire: 30000,
  28. idle: 10000
  29. },
  30. });
  31.  
  32. const app = express();
  33. app.models = initModels(sequelize);
  34. app.use(morgan());
  35. app.use(bodyParser.json());
  36. app.use(bodyParser.urlencoded({
  37. extended: false
  38. }));
  39. app.get('/api/v1/places', getPlacesIndex);
  40. app.get('/api/v1/wishlist', getWishlistIndex);
  41. app.post('/api/v1/wishlist_places', postWishlistPlaces);
  42. app.delete('/api/v1/wishlist_places/:id', deleteWishlistPlaces);
  43.  
  44. const onError = (error) => {
  45. if (error.syscall !== 'listen') throw error;
  46.  
  47. const porty = app.address;
  48.  
  49. const bind =
  50. typeof porty === 'string' ?
  51. `Pipe ${porty}` :
  52. `Port + ${porty}`;
  53.  
  54. switch (error.code) {
  55. case 'EACCES':
  56. debug(`${bind} requires elevated privileges`);
  57. process.exit(1);
  58. break;
  59. case 'EADDRINUSE':
  60. debug(`${bind} is already in use`);
  61. process.exit(1);
  62. break;
  63. default:
  64. throw error;
  65. }
  66. };
  67.  
  68. const onListening = () => {
  69. const addr = app.address();
  70. const bind =
  71. typeof addr === 'string' ?
  72. `Pipe ${addr}` :
  73. `Port ${addr.port}`;
  74.  
  75. debug(`Listening on ${bind}`);
  76. };
  77.  
  78. sequelize.sync()
  79. .then(() => {
  80. if (process.env.SEED_DATABASE) {
  81. plant(seeds, app.models);
  82. }
  83. })
  84. .then(() => {
  85. app.listen(3000, function() {
  86. console.log('**********************');
  87. console.log(' Doumi server running ');
  88. console.log('**********************');
  89. });
  90. app.on('error', onError);
  91. app.on('listening', onListening);
  92. })
  93. .catch((err) => {
  94. throw err;
  95. });
Add Comment
Please, Sign In to add comment