Skip to content

Commit

Permalink
refactor: error handling connection, use dotenv
Browse files Browse the repository at this point in the history
  • Loading branch information
rvpanoz committed Jan 30, 2020
1 parent 6887ede commit 70180a4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"body-parser": "^1.19.0",
"cookie-parser": "^1.4.4",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"mongodb": "^3.5.1",
Expand Down
14 changes: 11 additions & 3 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import dotenv from "dotenv";

const CONFIG = dotenv.config();
const {
parsed: { DB_USER, DB_PASSWORD, ...rest }
} = CONFIG;

const URI = `mongodb+srv://${DB_USER}:${DB_PASSWORD}@cluster0-diifo.mongodb.net/db_z0?retryWrites=true&w=majority`;

const config = {
port: 8000,
jwtExpirationSeconds: 300,
JWT_KEY: "my_secret"
URI,
...rest
};

export default config;
29 changes: 18 additions & 11 deletions src/db/connect.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
import mongoose from "mongoose";
import config from "../config";

const uri =
"mongodb+srv://agileactors:[email protected]/db_z0?retryWrites=true&w=majority";
const db = mongoose.connection;
const { URI } = config;

// setup mongoose
mongoose.Promise = global.Promise;
mongoose.set("useCreateIndex", true);

const connect = () => {
try {
mongoose.connect(uri, {
const db = mongoose.connection;
const resultP = new Promise((resolve, reject) => {
mongoose.connect(URI, {
useNewUrlParser: true,
useUnifiedTopology: true
});

//bind connection to error event (to get notification of connection errors)
db.on("error", console.error.bind(console, "MongoDB connection error:"));
db.on("error", error =>
reject({
success: false,
message: error
})
);

db.once("open", () => {
console.log("database connection: OK");
resolve({
success: true,
message: "database connection: OK"
});
});
} catch (error) {
console.error(error);
}
});

return resultP;
};

export default connect;
11 changes: 7 additions & 4 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import config from "./config";
import { connect } from "./db";
import { userRouter } from "./routers";

const { port } = config || 8000;
const { PORT } = config || 8000;
const app = express();

app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(userRouter);

app.listen(port, () => {
console.log(`Server is running at port: ${port}`);
connect();
app.listen(PORT, () => {
console.log(`Server is running at port: ${PORT}`);

connect()
.then(({ message }) => console.log(message))
.catch(error => console.log(error));
});

0 comments on commit 70180a4

Please sign in to comment.