35
loading...
This website collects cookies to deliver better user experience
USERNAME=databaseUsername
PASSWORD=doubleSecretDatabasePassword
const schema = {
type: 'object',
required: ['PASSWORD', 'USERNAME'],
properties: {
PASSWORD: {
type: 'string'
},
USERNAME: {
type: 'string'
}
}
}
const options = {
dotenv: true,
data: process.env
}
fastify.register(fastifyEnv, options)
await fastify.after()
// Now the .env variables are defined
// Fastify
const fastify = require('fastify')({
logger: true
})
const fastifyEnv = require('fastify-env')
const schema = {
type: 'object',
required: ['DB_PASSWORD', 'DB_USERNAME'],
properties: {
DB_PASSWORD: {
type: 'string'
},
DB_USERNAME: {
type: 'string'
}
}
}
const options = {
confKey: 'config',
schema,
dotenv: true,
data: process.env
}
const initialize = async () => {
fastify.register(fastifyEnv, options)
await fastify.after()
// Database
// Connection URL
const username = encodeURIComponent(fastify.config.DB_USERNAME)
const password = encodeURIComponent(fastify.config.DB_PASSWORD)
const dbName = 'databaseName'
const url = `mongodb://${username}:${password}@localhost:27017/${dbName}`
fastify.register(require('./database-connector'), {
url,
useUnifiedTopology: true
})
}
initialize()
// Fire up the server
(async () => {
try {
await fastify.ready()
await fastify.listen(process.env.PORT)
} catch (error) {
fastify.log.error(error)
process.exit(1)
}
})()