Skip to content

Instantly share code, notes, and snippets.

@overthemike
Created March 29, 2020 17:51
Show Gist options
  • Save overthemike/7e7d57ae6e6393e34c26dcbefab927e7 to your computer and use it in GitHub Desktop.
Save overthemike/7e7d57ae6e6393e34c26dcbefab927e7 to your computer and use it in GitHub Desktop.
import { Router } from "express"
import ejwt from "express-jwt"
import config from "config"
import createError from "http-errors"
import publicRouter from "./public"
import protectedRouter from "./protected"
console.log(protectedRouter)
const router = Router()
router.use(publicRouter)
router.use(ejwt({ secret: config.get("secret") }), protectedRouter)
router.use((req, res, next) => {
next(createError(404))
})
router.use((err, req, res, next) => {
res.status(err.status || 500)
res.json({
message: err.message,
error: err
})
})
export default router
import express from "express"
import http from "http"
import path from "path"
import cookieParser from "cookie-parser"
import logger from "morgan"
import onDeath from "death"
import config from "config"
import apiRouter from "./routes/api"
const app = express()
app.use(logger("dev"))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(express.static(path.join(__dirname, "public")))
// Error handling has to happen within the apiRouter
// so that production can serve the React application
// on any route that doesn't start with "/api"
app.use("/api", apiRouter)
app.get("*", (req, res, next) => {
res.sendFile(path.join(__dirname, "public/index.html"))
})
onDeath(signal => {
console.log("\nShutting down gracefully.")
process.exit()
})
const server = http.createServer(app)
server.listen(config.get("server.port"), () => {
console.log(`Server running on port ${config.get("server.port")}`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment