SHARE
TWEET

Untitled

a guest Aug 20th, 2019 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Import Modules
  2. import express from 'express'; // Express framework
  3. import dotenv from 'dotenv'; //Dotenv library
  4. import '@babel/polyfill'; // Regenator runtime
  5. import log from './logger'; // Custom logger
  6. import db from './db'; // Custom database
  7. import routes from './routes'; // Routes file
  8. import {
  9.   ALLOW_ORIGIN,
  10.   ALLOW,
  11.   ALLOW_HEADERS,
  12.   ALLOW_METHODS,
  13.   ALLOW_WILDCARD,
  14.   REST_HEADERS_OPTIONS,
  15.   REST_METHODS,
  16. } from './constants'; // Request headers
  17.  
  18. // Load env vars
  19. dotenv.config();
  20.  
  21. // import Exprees
  22. const app = express();
  23. // import Body-parser
  24. const bodyParser = require('body-parser');
  25.  
  26. // Get the application PORT from our env var
  27. const {
  28.   PORT,
  29. } = process.env;
  30.  
  31. // Define the elements that will use our middleware
  32.  
  33. // Define request headers, this will avoid CORS missconfiguration
  34. app.use((req, res, next) => {
  35.   res.header(ALLOW_ORIGIN, ALLOW_WILDCARD);
  36.   res.header(ALLOW_HEADERS, REST_HEADERS_OPTIONS);
  37.   res.header(ALLOW_METHODS, REST_METHODS);
  38.   res.header(ALLOW, REST_METHODS);
  39.   next();
  40. });
  41. // Avoid nested objects
  42. app.use(bodyParser.urlencoded({ extended: false }));
  43. // Read only JSON requests
  44. app.use(bodyParser.json());
  45. // Our API routes
  46. app.use(routes);
  47.  
  48. // Our application will start in the defined port
  49. app.listen(PORT, () => {
  50.   // Connect to database
  51.   db();
  52.   // Log that the application is running
  53.   log(`Node app is running on port ${PORT}`);
  54. });
  55.  
  56. // Export our API
  57. export default { app };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top