Created
October 16, 2020 17:36
-
-
Save notsonoobie/ba1526ecab649da79d9342c08a157f14 to your computer and use it in GitHub Desktop.
Starter template for NodeJS - ExpressJS setup.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require('path') | |
const express = require('express') | |
const mongoose = require('mongoose') | |
const helmet = require('helmet') // Express App Security - Headers | |
const cors = require('cors') | |
const morgan = require('morgan') // Logging Request | |
const yup = require('yup') // Schema Validations | |
const app = express() | |
const PORT = process.env.PORT || 5000 | |
app.use(express.json()) // Body Parser Middleware | |
app.use(cors({ | |
origin: "*", | |
optionsSuccessStatus: 200 | |
})) | |
app.use(helmet()) | |
app.use(morgan('common')) // Logging API Calls | |
// All API Routes | |
app.use(express.static('client/build')) // Middleware for serving static assets | |
app.get('*', (req,res) => { | |
res.sendFile(path.resolve(__dirname,'client','build','index.html')) // For CRA | |
}) | |
app.listen(PORT, (err) => { | |
if(err) return console.log('Error occured while starting the App') | |
console.log(`Server up and running at Port ${PORT}`) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment