Skip to content

Commit

Permalink
Merge pull request #9 from brayan9413/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
brayan9413 authored May 28, 2024
2 parents e03c32c + 2a635d1 commit 2ff954a
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export MONGO_URI=mongodb://localhost:27017/test_br_api

4. Start the server (dev mode)
```bash
npm start-dev
npm run start-dev
```

**start WITH DOCKER**
Expand Down
5 changes: 4 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ const express = require("express");
const bodyParser = require("body-parser");
const { routerUser } = require("./routes/routerUser");
const connectToDatabase = require("./database-config/dbConfig");
const logger = require("./logger/logger.js");

const app = express();
const port = 3000;

app.use(bodyParser.json());

app.get("/health-check", (req, res) => {
logger.info("Health check OK");
res.status(200).send("OK");
});

Expand All @@ -19,9 +21,10 @@ async function startServer() {
await connectToDatabase();

app.listen(port, () => {
console.log(`App running on port: ${port}`);
logger.info(`App running on port: ${port}`);
});
} catch (err) {
logger.error(err);
process.exit(1); // Exit the process if unable to connect to MongoDB
}
}
Expand Down
5 changes: 3 additions & 2 deletions database-config/dbConfig.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const mongoose = require("mongoose");
const logger = require("../logger/logger.js");

async function connectToDatabase() {
try {
await mongoose.connect(process.env.MONGO_URI, {});
console.log("Successfully connected to the database");
logger.info("Successfully connected to the database");
} catch (error) {
console.error("Error connecting to the database", error);
logger.error("Error connecting to the database", error);
throw error;
}
}
Expand Down
20 changes: 20 additions & 0 deletions logger/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const winston = require("winston");

const logger = winston.createLogger({
level: "info",
format: winston.format.combine(
winston.format.timestamp(),
winston.format.json(),
),
defaultMeta: { service: "node-sample-api" },
transports: [
//
// - Write all logs with importance level of `error` or less to `error.log`
// - Write all logs with importance level of `info` or less to `combined.log`
//
// new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.Console(),
],
});

module.exports = logger;
Loading

0 comments on commit 2ff954a

Please sign in to comment.