簡體   English   中英

Heroku錯誤:找不到模塊'./config/keys'

[英]Heroku Error: Cannot find module './config/keys'

我部署了一個Nodejs應用程序,它的后端到目前為止Heroku。 我在瀏覽器中收到應用程序錯誤。 當我運行heroku logs ,我看到了這個錯誤:

Error: Cannot find module './config/keys'

所以我運行一個heroku run 'ls -al' ,我看到了這個:

Running ls -al on ⬢ murmuring-temple-46226... up, run.3327 (Free)
total 284
drwx------   8 u6811 dyno   4096 Apr 21 11:41 .
drwxr-xr-x  15 root  root   4096 Apr 18 13:09 ..
-rw-------   1 u6811 dyno     21 Apr 21 11:37 .gitignore
drwx------   3 u6811 dyno   4096 Apr 21 11:37 .heroku
drwx------   2 u6811 dyno   4096 Apr 21 11:37 .profile.d
-rw-------   1 u6811 dyno      0 Apr 21 11:37 @1
-rw-------   1 u6811 dyno    574 Apr 21 11:37 index.js
drwx------   2 u6811 dyno   4096 Apr 21 11:37 models
drwx------ 261 u6811 dyno  12288 Apr 21 11:38 node_modules
-rw-------   1 u6811 dyno 235090 Apr 21 11:37 package-lock.json
-rw-------   1 u6811 dyno    565 Apr 21 11:37 package.json
drwx------   2 u6811 dyno   4096 Apr 21 11:37 routes
drwx------   2 u6811 dyno   4096 Apr 21 11:37 services

我確實看到我的config文件夾不在頂部的文件和文件夾列表中,但這可能是因為在我的.gitignore文件中,我有它所以它會忽略keys.js文件,或者它可能是我擁有它在我的代碼庫中引用錯誤?

這就是我在index.js需要它的方式:

const express = require('express');
const mongoose = require('mongoose');
const cookieSession = require('cookie-session');
const passport = require('passport');
const keys = require('./config/keys');
require('./models/User');
require('./services/passport');

mongoose.connect(keys.mongoURI);

const app = express();

app.use(
  cookieSession({
    maxAge: 30 * 24 * 60 * 60 * 1000,
    keys: [keys.cookieKey]
  })
);
app.use(passport.initialize());
app.use(passport.session());

require('./routes/authRoutes')(app);

const PORT = process.env.PORT || 5000;
app.listen(PORT);

這就是我在services/passport.js引用它的方式:

const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const mongoose = require('mongoose');
const keys = require('../config/keys');

const User = mongoose.model('users');

passport.serializeUser((user, done) => {
  done(null, user.id);
});

passport.deserializeUser((id, done) => {
  User.findById(id).then(user => {
    done(null, user);
  });
});

// passport.use() is a generic register to make Passport
// aware of new strategy
// creates a new instance to authenticate users
passport.use(
  new GoogleStrategy(
    {
      clientID: keys.googleClientID,
      clientSecret: keys.googleClientSecret,
      callbackURL: '/auth/google/callback'
    },
    (accessToken, refreshToken, profile, done) => {
      User.findOne({ googleId: profile.id }).then(existingUser => {
        if (existingUser) {
          // we already have a record with given profile id
          done(null, existingUser);
        } else {
          // we dont have a user record with this id, make a new record
          new User({ googleId: profile.id })
            .save()
            .then(user => done(null, user));
        }
      });
    }
  )
);

使用包含API密鑰和其他config/keys.jsconfig/keys.js文件夾文件結構將應用程序成功部署到Heroku的最佳做法是什么?

我要做的是從你的.gitignore文件中刪除keys.js config文件夾中創建另外兩個文件。 一個用於開發,一個用於生產。 將您的密鑰放在開發文件中,然后讓.gitignore忽略該開發文件。

然后在你的keys.js文件中創建一個if語句,如下所示:

// keys.js - figure out what set of credentials to return
if (process.env.NODE_ENV === 'production') {
  // we are in production - return the prod set of keys
  module.exports = require('./prod');
} else {
  // we are in development - return the dev keys!!
  module.exports = require('./dev');
}

然后在您的生產文件中,您將按照上面建議的同事那樣做,但可能更像是這樣:

// prod.js - production keys here
module.exports = {
  googleClientID: process.env.GOOGLE_CLIENT_ID,
  googleClientSecret: process.env.GOOGLE_CLIENT_SECRET,
  cookieKey: process.env.COOKIE_KEY
};

如果您還要連接到像MongoDB這樣的數據庫,那么您想要像上面這樣添加mongoURI: process.env.MONGO_URI

您要做的最后一件事是確保在Heroku環境變量上定義所有這些不同的環境變量。

設置所有這些內容非常簡單,您只需要知道在何處查看Herokus的控制界面,或者通過命令行終端進行操作,如上面的同事建議的那樣。 如果您對命令行終端非常熟悉,請繼續按照上述步驟操作,如果沒有,請進入瀏覽器並導航至dashboard.heroku.com

找到您為應用創建的應用程序,單擊它。

然后單擊設置選項卡,您將看到Config Variables 單擊顯示Config Variables,這將為您提供一個界面,用於設置要添加到應用程序的所有不同變量。

逐個添加一個鍵及其對應的值

首先執行GOOGLE_CLIENT_ID ,復制並輸入密鑰,然后獲取憑據。 您應該已經在某處粘貼了憑據副本。

COOKIE_KEY做同樣的COOKIE_KEY ,如果你使用的是Mongo,那就是MONGO_URI

所以現在你可以隱藏配置變量,沒有人能夠看到它們。

正如您可能想象的那樣,您不希望任何人進入您的heroku帳戶。 如果有人進入您的heroku帳戶,我不知道該告訴您什么。

現在,您可以通過使用git提交代碼來部署應用程序,並使用git進行部署。

執行顯示文件和文件夾的git status

git add .

git commit -m “completed environment variables”

git push

git push heroku master

你應該很高興去掉那個已經消失的錯誤。

你通過忽略配置文件做正確的事情。你應該總是從github和bitbucket中取走你的秘密密鑰。 在這種情況下,正確的方法是將您的密鑰設置為heroku上的環境變量。 您可以使用以下命令:

# Add new variable
heroku config:set COOKIE_KEY=somekey

# Remove variable
heroku config:unset COOKIE_KEY

# Get all environment variables
heroku config

# Get environment variable by name
heroku config:get COOKIE_KEY

在您的應用程序中,您可以使用process.env對象訪問這些變量:

process.env.COOKIE_KEY
process.env.GOOGLE_CLIENT_ID
process.env.GOOGLE_CLIENT_SECRET

注意:配置鍵必須為大寫

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM