Final Server File Step -13

Final Server File Step -13

Step 13:-Final Server File

// Import Dependency
const dotenv = require ('dotenv');
const connectDB= require('./config/db');
const express = require('express');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const errorHandler= require('./middleware/error');
const fileupload = require('express-fileupload');
const path= require('path');

//Load environment variable into an object
dotenv.config({path:'./config/config.env'});

//connect to database
connectDB();

// Route Files
const route = require('./routes/teamMember');

//Intialize App variable with express
const app =express();

//configure bodyparser to hande the post requests
app.use(bodyParser.urlencoded({ extended: true}));
app.use(bodyParser.json())

//Dev logging middleware
if(process.env.NODE_ENV=="DEVELOPMENT")
{
    app.use(morgan('dev'))
}
//File upload
app.use(fileupload());

//static folder
app.use(express.static(path.join(__dirname,'public')));

//Mount route
app.use('/api',route);

//Error Handling GUI
app.use(errorHandler);
//to run server
const PORT= process.env.PORT || 5000;
const server=app.listen(
    PORT,
    console.log(`server running in ${process.env.NODE_ENV} mode using port  ${PORT}`));

process.on('unhandledRejection',(err,promise)=>{
    console.log(`Error: ${err.message}`);
    server.close(()=>process.exit(1));
})

Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

*

*