@conjurelabs/mode
v0.2.1
Published
express server scaffolding
Downloads
4
Readme
Mode
Quick setup scaffolding for an Express server
Install
npm -i --save @conjurelabs/route @conjurelabs/utils @conjurelabs/mode
Usage
Add a directory of routes, in ./routes
within your project root.
const mode = require('@conjurelabs/mode')
mode()
This will run an Express sever on :3000
See the route module documenation for more deatils on using routes.
Routes within a mode
server can access the route
module via @conjurelabs/mode/route
.
// within './routes/test/get.js'
const Route = require('@conjurelabs/mode/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.
Advanced Usage
Name
You can set a more specific name for your server. This makes logs more sensical when dealing with multiple servers.
mode({
name: 'API'
})
Routes Directory
By default mode
will crawl routes at ./routes
within the project root. You can override that.
mode({
routesDir: '/custom/routes/dir'
})
Port
By default mode
runs on port 3000
mode({
port: 4400
})
Adding more Express configuration
After mode
finishes configuring the express server, you can add your own addidtional config.
mode({
serverAfterConfig: (server, express) => {
server.use(express.static('public'))
server.use(passport.initialize())
server.use(passport.session())
}
})
Before server starts
This is useful for wrapping .listen
with additional logic, like when wrapping Express in a Next app.
mode({
beforeListen: (server, express, done) => {
// start next app
nextApp
.prepare()
.then(done)
}
})
After server starts
mode({
afterListen: (server, express) => {
// ...
}
})