Created
December 29, 2018 12:03
-
-
Save tarusharora/1af68e63a6ef5abd7e7f843737bc8fb8 to your computer and use it in GitHub Desktop.
Server with fastify autoload
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 dependencies from npm | |
const Fastify = require('fastify'); | |
const path = require('path'); | |
const AutoLoad = require('fastify-autoload'); | |
const uuidv4 = require('uuid/v4'); | |
// create request ids | |
const createRequestId = () => uuidv4(); | |
const createServer = (options) => { | |
const { logSeverity } = options; | |
// create the server | |
const server = Fastify({ | |
ignoreTrailingSlash: true, | |
logger: { | |
genReqId: createRequestId, | |
level: logSeverity | |
} | |
}); | |
// register the plugins, routes in this case | |
server.register(AutoLoad, { | |
dir: path.join(__dirname, 'api', 'routes') | |
}); | |
// start the server | |
server.listen(9000, (err) => { | |
if (err) { | |
server.log.error(err); | |
console.log(err); | |
process.exit(1); | |
} | |
server.log.info('Server Started'); | |
}); | |
} | |
module.exports = { | |
createServer | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment