我正在进行fastify微服务,并希望使用fastify-env库来验证我的env输入,并在整个应用程序中提供默认设置。
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()如何在服务器启动之前使用fastify.config对象?
发布于 2018-09-08 17:18:04
fastify.register异步加载插件。如果您想立即使用特定插件中的内容,请使用:
fastify
.register(plugin)
.after(() => {
// This particular plugin is ready!
});发布于 2020-05-14 20:49:46
使用ready() https://www.fastify.io/docs/latest/Server/#ready等待加载所有插件。然后使用配置变量调用listen()。
try {
await fastify.ready(); // will load all plugins
await fastify.listen(...);
} catch (err) {
fastify.log.error(err);
process.exit(1);
}https://stackoverflow.com/questions/50731931
复制相似问题