const Fastify = require('fastify').default,
server = Fastify(),
dotenv = require('@hadimardanian/fastify-dotenv');
server.register(dotenv, {
path: '/config/.env', // (1)
decorator: 'env' // (2)
});
server.listen(3000, function () {
console.log('server is running on port ' + fastify.env);
});
- (1) path : the "/" means the root of project folder. it must be at the beginning of path.
- (2) decorator : name it whatever you want, if omitted it will be "env" and if set it to
false
will not set. - the ".env" file: Its name could be whatever you want. but its format must be plain text and content must be like following:
PORT=3000
MONGO_CONNECTION=mongodb://localhost/mydb
NODE_ENV=production
And in general ...
KEY=VALUE
const Fastify = require('fastify').default,
server = Fastify(),
dotenv = require('@hadimardanian/fastify-dotenv');
server.register(dotenv, {
path: '/config/.env',
root: '/home/me/Documents/projects/nodeApp'
});
server.listen(3000, function () {
console.log('server is running on port ' + fastify.env);
});