const ejs = require('ejs')
const path = require('path');
const fastify = require('fastify')();
const fastify_static = require('fastify-static');
const fastify_autoload = require('fastify-autoload');
const fastify_mongoose = require('fastify-mongoose');
const fastify_env = require('fastify-env');
let PORT;
let uri;
fastify.register(fastify_env, {
dotenv: true,
schema: {
type: 'object',
required: ['MONGO_URI', 'PORT'],
properties: {
MONGO_URI: {
type: 'string',
default: ''
},
PORT: {
type: 'string',
default: ''
}
}
}
}).ready(err => {
if (err) {
throw new Error(err);
}
PORT = fastify.config.PORT;
uri = fastify.config.MONGO_URI;
})
fastify.register(fastify_static, {
root: path.join(__dirname, 'public'),
})
fastify.register(require('point-of-view'), {
engine: {
ejs: ejs,
},
root: path.join(__dirname, 'view')
})
fastify.register(fastify_autoload, {
dir: path.join(__dirname, 'Logic/Routes'),
})
fastify.register(require('./Logic/Plugins/cache'))
fastify.register(fastify_mongoose, {
uri: uri
})
const start = async () => {
try {
await fastify.ready();
await fastify.listen(process.env.PORT);
console.log('Listening on port: ', PORT);
} catch (error) {
throw new Error(error);
}
}
start();错误:错误:在D:\Users\Antonio\Desktop\Antonio\Work\GitHub\NextLevel\src\main.js:30:15中,uri参数是强制性的
问题在于法西斯-猫鼬,或者可能是法西斯-env。我解释说:如果我在URI参数中直接写入URI,它就能工作。但是使用环境变量不..。
发布于 2022-01-23 20:30:37
您是否使用dotenv,因为您应该使用,我不建议您在代码中添加密码或访问API。
require('dotenv').config()让我们记住,当我们开始
start()我们必须调用fastify和dotenv,否则将不包括process.env.YOUR_VARIABLE。
https://stackoverflow.com/questions/69807172
复制相似问题