Quick setup scaffolding for an Express server
npm -i --save @conjurelabs/route @conjurelabs/utils @conjurelabs/express-abridged
Add a directory of routes, in ./routes
within your project root.
const abridged = require('@conjurelabs/express-abridged')
abridged()
This will run an Express sever on :3000
See the route module documenation for more deatils on using routes.
Routes within a abridged
server can access the route
module via @conjurelabs/express-abridged/route
.
// within './routes/test/get.js'
const Route = require('@conjurelabs/express-abridged/route')
const route = new Route()
route.push(async (req, res) => {
res.send('Hello')
})
module.exports = route
// this will allow a user to GET /test on the running server
See a working example.
You can set a more specific name for your server. This makes logs more sensical when dealing with multiple servers.
abridged({
name: 'API'
})
By default abridged
will crawl routes at ./routes
within the project root. You can override that.
abridged({
routesDir: '/custom/routes/dir'
})
By default abridged
runs on port 3000
abridged({
port: 4400
})
After abridged
finishes configuring the express server, you can add your own addidtional config.
abridged({
serverAfterConfig: (server, express) => {
server.use(express.static('public'))
server.use(passport.initialize())
server.use(passport.session())
}
})
This is useful for wrapping .listen
with additional logic, like when wrapping Express in a Next app.
abridged({
beforeListen: (server, express, done) => {
// start next app
nextApp
.prepare()
.then(done)
}
})
abridged({
afterListen: (server, express) => {
// ...
}
})