如何同步加载fastify环境?
我正在使用fastify microservice,希望使用fastify env库验证我的env输入,并在整个应用程序中提供默认值如何同步加载fastify环境?,fastify,Fastify,我正在使用fastify microservice,希望使用fastify env库验证我的env输入,并在整个应用程序中提供默认值 const fastify = require('fastify')() fastify.register(require('fastify-env'), { schema: { type: 'object', properties: { PORT: { type: 'string', default: 3000 } }
const fastify = require('fastify')()
fastify.register(require('fastify-env'), {
schema: {
type: 'object',
properties: {
PORT: { type: 'string', default: 3000 }
}
}
})
console.log(fastify.config) // undefined
const start = async opts => {
try {
console.log('config', fastify.config) // config undefined
await fastify.listen(3000, '::')
console.log('after', fastify.config) // after { PORT: '3000' }
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
在服务器启动之前,如何使用
fastfy.config
对象?fastfy.register
异步加载插件。如果您想立即使用特定插件中的内容,请使用:
fastify
.register(plugin)
.after(() => {
// This particular plugin is ready!
});
使用ready()
等待加载所有插件。然后使用配置变量调用listen()
try {
await fastify.ready(); // will load all plugins
await fastify.listen(...);
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
你得到答案了吗?你还在使用fastify env吗?