송용우

Init MongoDB

1 SERVER_PORT=4000 1 SERVER_PORT=4000
2 +MONGO_URL=mongodb://localhost:27017/jaksimsamil
...\ No newline at end of file ...\ No newline at end of file
......
1 const express = require("express"); 1 const express = require("express");
2 const morgan = require("morgan"); 2 const morgan = require("morgan");
3 +const mongoose = require("mongoose");
3 const app = express(); 4 const app = express();
4 require("dotenv").config(); 5 require("dotenv").config();
5 - 6 +const { SERVER_PORT, MONGO_URL } = process.env;
6 app.use( 7 app.use(
7 morgan("[:date[iso]] :method :status :url :response-time(ms) :user-agent") 8 morgan("[:date[iso]] :method :status :url :response-time(ms) :user-agent")
8 ); 9 );
...@@ -10,6 +11,14 @@ app.use(express.json()); ...@@ -10,6 +11,14 @@ app.use(express.json());
10 app.use(express.urlencoded({ extended: false })); 11 app.use(express.urlencoded({ extended: false }));
11 app.use("/api", require("./api")); 12 app.use("/api", require("./api"));
12 13
13 -app.listen(process.env.SERVER_PORT, () => { 14 +mongoose
15 + .connect(MONGO_URL, { useNewUrlParser: true, useFindAndModify: false })
16 + .then(() => {
17 + console.log("Connected to MongoDB");
18 + })
19 + .catch((e) => {
20 + console.log(e);
21 + });
22 +app.listen(SERVER_PORT, () => {
14 console.log("Server is running on port", process.env.SERVER_PORT); 23 console.log("Server is running on port", process.env.SERVER_PORT);
15 }); 24 });
......
...@@ -4,9 +4,11 @@ ...@@ -4,9 +4,11 @@
4 "main": "index.js", 4 "main": "index.js",
5 "license": "MIT", 5 "license": "MIT",
6 "dependencies": { 6 "dependencies": {
7 + "dotenv": "^8.2.0",
7 "eslint-config-prettier": "^6.11.0", 8 "eslint-config-prettier": "^6.11.0",
8 "express": "^4.17.1", 9 "express": "^4.17.1",
9 "fs": "^0.0.1-security", 10 "fs": "^0.0.1-security",
11 + "mongoose": "^5.9.17",
10 "morgan": "^1.10.0", 12 "morgan": "^1.10.0",
11 "path": "^0.12.7" 13 "path": "^0.12.7"
12 }, 14 },
......
This diff is collapsed. Click to expand it.