Skip to content

Instantly share code, notes, and snippets.

@viral-sh
Created October 29, 2018 11:26
Show Gist options
  • Save viral-sh/4f7162819df26e9c8c19fa5ccf77608f to your computer and use it in GitHub Desktop.
Save viral-sh/4f7162819df26e9c8c19fa5ccf77608f to your computer and use it in GitHub Desktop.
Express Real world app
const express = require('express')
// ...all imports & requires here
const app = express()
const PORT = 3000
// add middlewares
app.use(helmet())
app.use(compression())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(cors())
app.use(fileUpload())
// add router middlewares
protectedRouter.use(authMiddleware())
adminRouter.use(adminMiddleware())
// add routers
app.use('/', publicRouter)
app.use('/protected', protectedRouter)
app.use('/admin', adminRouter)
// register APIs
registerApis(publicRouter, protectedRouter, adminRouter)
// add error handlers
app.use(errorHandler404)
app.use(notifyErrorHandler)
app.use(globalErrorHandler)
app.listen(port, () => console.log(`Realworld app listening on port ${port}!`))
@alaztetik
Copy link

Hello,
Above, in 'add routers' part, don't you have to use callback functions as publicRouter(), protectedRouter() and adminRouter()? Some documentations recommend that way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment