From 4abe7d18a747074439d47490aeff374e9791a294 Mon Sep 17 00:00:00 2001 From: soohwanpak Date: Sun, 3 Nov 2024 23:02:23 +0900 Subject: [PATCH] =?UTF-8?q?dotenv=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- express-psh-sprint-6/data/seeds.js | 7 +++++-- express-psh-sprint-6/env.js | 2 -- express-psh-sprint-6/server.js | 8 +++++--- package-lock.json | 15 ++++++++++++++- package.json | 5 +++-- 6 files changed, 29 insertions(+), 11 deletions(-) delete mode 100644 express-psh-sprint-6/env.js diff --git a/.gitignore b/.gitignore index f74fb16d..50f204a4 100644 --- a/.gitignore +++ b/.gitignore @@ -10,12 +10,13 @@ lerna-debug.log* node_modules /react-psh-sprint5/node_modules/ /express-psh-sprint6/node_modules/ -/express-psh-sprint6/env.js +/express-psh-sprint6/.env /react-psh-sprint5/src/features/api/url.jsx dist dist-ssr *.local +.env # Editor directories and files .vscode/* !.vscode/extensions.json diff --git a/express-psh-sprint-6/data/seeds.js b/express-psh-sprint-6/data/seeds.js index 56f112b4..407fb05d 100644 --- a/express-psh-sprint-6/data/seeds.js +++ b/express-psh-sprint-6/data/seeds.js @@ -1,9 +1,12 @@ import mongoose, { mongo } from "mongoose"; import { Data } from "./mockData.js"; -import { DATABASE_URL } from "../env.js"; +import dotenv from 'dotenv'; import Product from "../models/Product.js"; -mongoose.connect(DATABASE_URL); +dotenv.config(); +const dbUrl = process.env.DATABASE_URL; + +mongoose.connect(dbUrl); await Product.deleteMany({}); await Product.insertMany(Data.list); diff --git a/express-psh-sprint-6/env.js b/express-psh-sprint-6/env.js deleted file mode 100644 index 36ec5caf..00000000 --- a/express-psh-sprint-6/env.js +++ /dev/null @@ -1,2 +0,0 @@ -export const DATABASE_URL = -"mongodb+srv://qoiuytnghks4:n6klkCFbRQhFOUpg@cluster0.ymmyl.mongodb.net/sprint6-db?retryWrites=true&w=majority&appName=Cluster0"; \ No newline at end of file diff --git a/express-psh-sprint-6/server.js b/express-psh-sprint-6/server.js index e4beba80..37123e80 100644 --- a/express-psh-sprint-6/server.js +++ b/express-psh-sprint-6/server.js @@ -1,16 +1,18 @@ import express from "express"; import cors from "cors"; import router from "./controllers/routes/routes.js"; -import { DATABASE_URL } from "./env.js"; +import dotenv from 'dotenv'; import mongoose from "mongoose"; const app = express(); app.use(cors()); - app.use(express.json()); +dotenv.config(); +const dbUrl = process.env.DATABASE_URL; + mongoose - .connect(DATABASE_URL) + .connect(dbUrl) .then(() => console.log("MongoDB connected")) .catch((err) => console.log(err)); diff --git a/package-lock.json b/package-lock.json index f05583b7..ed19d87d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,7 +5,8 @@ "packages": { "": { "dependencies": { - "concurrently": "^9.0.1" + "concurrently": "^9.0.1", + "dotenv": "^16.4.5" } }, "node_modules/ansi-regex": { @@ -117,6 +118,18 @@ "url": "https://github.com/open-cli-tools/concurrently?sponsor=1" } }, + "node_modules/dotenv": { + "version": "16.4.5", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.5.tgz", + "integrity": "sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, "node_modules/emoji-regex": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz", diff --git a/package.json b/package.json index be6b0319..5241730e 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,9 @@ { "dependencies": { - "concurrently": "^9.0.1" + "concurrently": "^9.0.1", + "dotenv": "^16.4.5" }, "scripts": { "start": "concurrently \"cd express-psh-sprint-6 && npm run dev\" \"cd react-psh-sprint-5 && npm run dev\"" } -} \ No newline at end of file +}