Skip to content

Instantly share code, notes, and snippets.

@notsonoobie
Created October 16, 2020 17:36
Show Gist options
  • Save notsonoobie/ba1526ecab649da79d9342c08a157f14 to your computer and use it in GitHub Desktop.
Save notsonoobie/ba1526ecab649da79d9342c08a157f14 to your computer and use it in GitHub Desktop.
Starter template for NodeJS - ExpressJS setup.
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