Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Import Modules
- import express from 'express'; // Express framework
- import dotenv from 'dotenv'; //Dotenv library
- import '@babel/polyfill'; // Regenator runtime
- import log from './logger'; // Custom logger
- import db from './db'; // Custom database
- import routes from './routes'; // Routes file
- import {
- ALLOW_ORIGIN,
- ALLOW,
- ALLOW_HEADERS,
- ALLOW_METHODS,
- ALLOW_WILDCARD,
- REST_HEADERS_OPTIONS,
- REST_METHODS,
- } from './constants'; // Request headers
- // Load env vars
- dotenv.config();
- // import Exprees
- const app = express();
- // import Body-parser
- const bodyParser = require('body-parser');
- // Get the application PORT from our env var
- const {
- PORT,
- } = process.env;
- // Define the elements that will use our middleware
- // Define request headers, this will avoid CORS missconfiguration
- app.use((req, res, next) => {
- res.header(ALLOW_ORIGIN, ALLOW_WILDCARD);
- res.header(ALLOW_HEADERS, REST_HEADERS_OPTIONS);
- res.header(ALLOW_METHODS, REST_METHODS);
- res.header(ALLOW, REST_METHODS);
- next();
- });
- // Avoid nested objects
- app.use(bodyParser.urlencoded({ extended: false }));
- // Read only JSON requests
- app.use(bodyParser.json());
- // Our API routes
- app.use(routes);
- // Our application will start in the defined port
- app.listen(PORT, () => {
- // Connect to database
- db();
- // Log that the application is running
- log(`Node app is running on port ${PORT}`);
- });
- // Export our API
- export default { app };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement