【问题标题】:Mailgun not seeing credentials in Node.js appMailgun 在 Node.js 应用程序中看不到凭据
【发布时间】:2021-10-27 17:30:28
【问题描述】:

我遇到了一个问题,当我将 Mailgun 上传到 Heroku 时,它没有看到 apiKey。我在代码顶部设置了环境变量和 dotenv,但它仍然返回错误。我有什么遗漏吗?

  • 在我的 app.js 中
require('dotenv').config();
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const app = express();
// MAILGUN INFO
let api_key = process.env.MAILGUN_API_KEY
let domain = process.env.DOMAIN 
var mailgun = require('mailgun-js')({apiKey: api_key, domain: domain});

//DATA PARSING AND VIEW ENGINE
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
app.set("view engine", 'ejs');
  • 我的 dotenv 信用卡
MAILGUN_API_KEY=******************************
DOMAIN=*********.com

  • 日志中返回错误
2021-08-27T20:07:53.000000+00:00 app[api]: Build succeeded
2021-08-27T20:07:58.685692+00:00 heroku[web.1]: Starting process with command `node app.js`
2021-08-27T20:08:00.618937+00:00 app[web.1]: /app/node_modules/mailgun-js/lib/mailgun.js:16
2021-08-27T20:08:00.618949+00:00 app[web.1]: throw new Error('apiKey value must be defined!')
2021-08-27T20:08:00.618949+00:00 app[web.1]: ^
2021-08-27T20:08:00.618949+00:00 app[web.1]:
2021-08-27T20:08:00.618950+00:00 app[web.1]: Error: apiKey value must be defined!
2021-08-27T20:08:00.618950+00:00 app[web.1]: at new Mailgun (/app/node_modules/mailgun-js/lib/mailgun.js:16:13)
2021-08-27T20:08:00.618950+00:00 app[web.1]: at create (/app/node_modules/mailgun-js/lib/mailgun.js:239:10) 
2021-08-27T20:08:00.618950+00:00 app[web.1]: at Object.<anonymous> (/app/app.js:11:36)
2021-08-27T20:08:00.618951+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1063:30)    
2021-08-27T20:08:00.618951+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
2021-08-27T20:08:00.618952+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:928:32)
2021-08-27T20:08:00.618952+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:769:14)
2021-08-27T20:08:00.618952+00:00 app[web.1]: at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
2021-08-27T20:08:00.618953+00:00 app[web.1]: at internal/main/run_main_module.js:17:47
2021-08-27T20:08:00.666412+00:00 heroku[web.1]: Process exited with status 1
2021-08-27T20:08:00.719053+00:00 heroku[web.1]: State changed from starting to crashed

【问题讨论】:

    标签: node.js heroku error-handling mailgun


    【解决方案1】:

    您的.env 文件只能用于本地配置。

    “.env 文件可让您捕获在本地运行应用程序所需的所有配置变量。” -- https://devcenter.heroku.com/articles/heroku-local

    在部署到 Heroku 的服务器上时,使用 heroku_config 命令或 Heroku 的 GUI 为您的应用设置环境变量。更多详情请见Heroku's website

    【讨论】:

    • 谢谢!这正是我需要做的!
    • @LeonSmithJr。伟大的! ?
    猜你喜欢
    • 2020-07-11
    • 2015-03-29
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    • 2018-04-06
    相关资源
    最近更新 更多