首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >fastify猫鼬不起作用,node.js和fastify

fastify猫鼬不起作用,node.js和fastify
EN

Stack Overflow用户
提问于 2021-11-02 08:15:26
回答 1查看 170关注 0票数 0
代码语言:javascript
复制
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,它就能工作。但是使用环境变量不..。

EN

回答 1

Stack Overflow用户

发布于 2022-01-23 20:30:37

您是否使用dotenv,因为您应该使用,我不建议您在代码中添加密码或访问API。

代码语言:javascript
复制
require('dotenv').config()

让我们记住,当我们开始

代码语言:javascript
复制
 start()

我们必须调用fastify和dotenv,否则将不包括process.env.YOUR_VARIABLE。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69807172

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档