[英]Can't access API-key from .env-file
We are having problem accessing the API-key from our.env file when trying to fetch in our server.js.尝试获取 server.js 时,我们在从 our.env 文件访问 API 密钥时遇到问题。 If we add the API-key manually to the URL in server.js it works, so the problem seems to be the connection between server.js and.env-file.
如果我们手动将 API 密钥添加到 server.js 中的 URL 它会起作用,所以问题似乎是 server.js 和 .env 文件之间的连接。 We have npm installed dotenv.
我们已经安装了 npm dotenv。 In the.env file we have written the key like this:
WEATHER_API_KEY = XXXXXXXXXXXX
在 .env 文件中我们这样写了密钥:
WEATHER_API_KEY = XXXXXXXXXXXX
Does anyone know what we have done wrong?有谁知道我们做错了什么?
import express from "express";
import cors from "cors";
import mongoose from "mongoose";
import crypto from "crypto";
import bcrypt from "bcrypt";
import request from "request";
import dotenv from "dotenv";
// import { stringify } from "querystring";
const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/project-mongo";
mongoose.connect(mongoUrl, { useNewUrlParser: true, useUnifiedTopology: true });
mongoose.Promise = Promise;
const port = process.env.PORT || 8080;
const app = express();
dotenv.config();
app.get("/home", (req, res) => {
let city = req.query.city;
// const request = require("request");
// const options = {
// url: `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${process.env.WEATHER_API_KEY}`,
// method: "GET",
// headers: {
// Accept: "application/json",
// },
// };
const key = "*******************";
const requesturl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${key}`;
request(requesturl, function (error, response, body) {
let data = JSON.parse(body);
console.log(response);
if (response.statusCode === 200) {
res.send(`The weather in ${city} is ${data.weather[0].description}`);
} else {
res.send(data.message);
}
});
console.log(process.env.WEATHER_API_KEY);
});
You may try this你可以试试这个
import 'dotenv/config';
in place of import dotenv from "dotenv";
代替
import dotenv from "dotenv";
and remove the dotenv.config();
并删除
dotenv.config();
call.称呼。
Source and explanation: https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import来源和解释: https ://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import
And update the request URL (which you might have changed for testing purpose) to并将请求 URL(您可能出于测试目的而更改)更新为
const requesturl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${process.env.key}`;
Also, try defining your key without any spaces, though this is less likely to be the root cause.此外,请尝试在没有任何空格的情况下定义您的密钥,尽管这不太可能是根本原因。
WEATHER_API_KEY="XXXXXXXXXXXX"
Although the question is not specifically about React, it might be helpful for those who use React.虽然这个问题不是专门针对 React 的,但它可能对那些使用 React 的人有所帮助。 For a React App, Environment Variables have to start with the REACT_APP_ prefix otherwise it won't work.
对于 React 应用程序,环境变量必须以 REACT_APP_ 前缀开头,否则它将无法工作。
REACT_APP_WEATHER_API_KEY="XXXXXXXXXXXX"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.