Skip to content

Commit

Permalink
Added DB and Mongoose
Browse files Browse the repository at this point in the history
- Env file to store env variables
- Added MongoDB and Mongoose
  • Loading branch information
felixtanhm committed Feb 21, 2024
1 parent 87465f3 commit a8d7bce
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 9 deletions.
3 changes: 2 additions & 1 deletion full-stack-javascript/mini-msg-board/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
.env
32 changes: 25 additions & 7 deletions full-stack-javascript/mini-msg-board/bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,43 @@
const app = require("../app");
const debug = require("debug")("mini-msg-board:server");
const http = require("http");
const mongoose = require("mongoose");
require("dotenv").config();

/**
* Get port from environment and store in Express.
* Create HTTP server.
*/

const port = normalizePort(process.env.PORT || "3000");
app.set("port", port);
const server = http.createServer(app);

/**
* Create HTTP server.
* Get port from environment and store in Express.
*/

const server = http.createServer(app);
const port = normalizePort(process.env.PORT || "3000");
app.set("port", port);

// Get DB variables from environment.
const mongoDb = process.env.MONGO_DB;
const mongoURI = process.env.MONGO_URI;
const uri = `${mongoURI}/${mongoDb}?retryWrites=true&w=majority`;

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
const connectDB = (async () => {
try {
const connectResult = await mongoose.connect(uri);
console.log("db connected");
server.listen(port, () => {
console.log("server running");
});
return connectResult;
} catch (err) {
onError(err);
}
})();
// server.listen(port);
server.on("error", onError);
server.on("listening", onListening);

Expand Down
227 changes: 227 additions & 0 deletions full-stack-javascript/mini-msg-board/package-lock.json

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

5 changes: 4 additions & 1 deletion full-stack-javascript/mini-msg-board/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
"version": "0.0.0",
"private": true,
"scripts": {
"dev": "node --watch ./bin/www"
"dev": "node --watch ./bin/www",
"start": "node ./bin/www"
},
"dependencies": {
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"dotenv": "^16.4.5",
"ejs": "~2.6.1",
"express": "~4.16.1",
"http-errors": "~1.6.3",
"mongoose": "^8.1.3",
"morgan": "~1.9.1"
}
}

0 comments on commit a8d7bce

Please sign in to comment.