Skip to content

Commit 914dfda

Browse files
committed
Header middleware and Code refactor
1 parent 36b89af commit 914dfda

File tree

4 files changed

+24
-18
lines changed

4 files changed

+24
-18
lines changed

app.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const express = require("express");
2-
const bodyParser = require("body-parser"); // Required for parsing request bodies
2+
const bodyParser = require("body-parser");
33
const cors = require("cors");
44
const helmet = require("helmet");
55
const morgan = require("morgan");
@@ -9,6 +9,7 @@ const Song = require("./models/Song");
99
const Album = require("./models/Album");
1010
const { getAlbum } = require("./controllers/albumController");
1111
const { getSong } = require("./controllers/songController");
12+
const setHeaderInformation = require("./middlewares/setHeaderInformation");
1213
const app = express();
1314
const port = 3000;
1415

@@ -20,16 +21,10 @@ app.use(helmet());
2021
app.use(morgan("dev"));
2122
app.use(bodyParser.json()); // Parse JSON requests
2223

23-
// Specify the path to the images folder
24-
const imagesPath = path.join(__dirname, "/public/images");
25-
// Serve static files from the images folder
26-
app.use("/images", express.static(imagesPath));
24+
app.use("/images", express.static(path.join(__dirname, "/public/images"))); // Serve static files from the images folder
2725

28-
// Set EJS as the view engine
29-
app.set("view engine", "ejs");
30-
31-
// Specify the directory where your views/templates are located (optional)
32-
app.set("views", path.join(__dirname, "views"));
26+
app.set("view engine", "ejs"); // Serve static files from the images folder
27+
app.set("views", path.join(__dirname, "views")); // Specify the views directory
3328

3429
app.get("/", async (req, res) => {
3530
const githubLink = "https://github.com/lakshaykamat/taylor-swift-api";
@@ -42,10 +37,11 @@ app.get("/", async (req, res) => {
4237
appName: "Taylor Swift API",
4338
});
4439
});
40+
app.use(setHeaderInformation); // Set Header information to all routes
4541

46-
// Use routes from separate files
4742
app.use("/albums", require("./routes/album"));
4843
app.use("/songs", require("./routes/song"));
44+
app.use("/quotes", require("./routes/quote"));
4945

5046
app.get("/album/:name", getAlbum);
5147
app.get("/song/:name", getSong);

controllers/albumController.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
const Album = require("../models/Album");
22
const Song = require("../models/Song");
3-
const {
4-
isAdmin,
5-
calculateTotalDuration,
6-
cleanUpSongsData,
7-
enhanceAlbumWithSongsData,
8-
} = require("../utils");
3+
const { isAdmin, enhanceAlbumWithSongsData } = require("../utils");
94
const errorHandler = require("../middlewares/errorMiddleware");
105

116
const getAlbum = async (req, res) => {

middlewares/errorMiddleware.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const errorHandler = (res, error, status = 500) => {
1+
const errorHandler = (message = null, res, error, status = 500) => {
2+
if (message) console.log(message);
23
console.error("Error:", error.message);
34
res.status(status).json({ error: "Internal Server Error" });
45
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function setHeaderInformation(req, res, next) {
2+
res.setHeader("Access-Control-Allow-Origin", "*");
3+
res.setHeader(
4+
"Access-Control-Allow-Methods",
5+
"GET, POST, OPTIONS, PUT, PATCH, DELETE"
6+
);
7+
res.setHeader(
8+
"Access-Control-Allow-Headers",
9+
"X-Requested-With,content-type"
10+
);
11+
next();
12+
}
13+
14+
module.exports = setHeaderInformation;

0 commit comments

Comments
 (0)