Created
March 29, 2020 17:51
-
-
Save overthemike/7e7d57ae6e6393e34c26dcbefab927e7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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 |
This file contains hidden or 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
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