From b5e069dca0161051688bc07b2e5df6b4e0445cbb Mon Sep 17 00:00:00 2001 From: Doris <107772913+Doris-Siu@users.noreply.github.com> Date: Sun, 3 Mar 2024 02:28:20 +0000 Subject: [PATCH] upload Dockerfile --- server/.dockerignore | 2 + server/Dockerfile | 8 +++ server/server.js | 125 ++++++++++++++++++++++--------------------- 3 files changed, 74 insertions(+), 61 deletions(-) create mode 100644 server/.dockerignore create mode 100644 server/Dockerfile diff --git a/server/.dockerignore b/server/.dockerignore new file mode 100644 index 0000000000..37d7e73486 --- /dev/null +++ b/server/.dockerignore @@ -0,0 +1,2 @@ +node_modules +.env diff --git a/server/Dockerfile b/server/Dockerfile new file mode 100644 index 0000000000..5ee4b3d84d --- /dev/null +++ b/server/Dockerfile @@ -0,0 +1,8 @@ +FROM node:18-alpine +WORKDIR /app +COPY package.json package-lock.json ./ +RUN npm install +RUN npm install dotenv +COPY . . +EXPOSE 5000 +CMD ["node", "server.js"] \ No newline at end of file diff --git a/server/server.js b/server/server.js index e40d1f666e..5536a6db09 100644 --- a/server/server.js +++ b/server/server.js @@ -20,6 +20,70 @@ const db = new Pool({ port: 5432 }); +//test11 +app.get("/", (req, res) => { + res.json("hi"); + + db.query("SELECT * FROM videos", (error, result) => { + console.log(error); + res.json(result.rows); + }); +}); + +// POST "/" +let idCount = 1; +app.post("/", function (req, res) { + const newVideo = req.body; + + const video = { + id: idCount++, + title: newVideo.title, + url: newVideo.url, + }; + + if (!newVideo.title || !newVideo.url) { + res.status(400).json({ + result: "failure", + message: "Video could not be saved", + }); + } else { + const query = + "INSERT INTO videos (id, title, url) " + "VALUES ($1, $2, $3)"; + + db.query(query, [video.id, video.title, video.url], (err, result) => { + res.status(201).json({ id: video.id }); + }); + } +}); + +//GET "/{id}" +app.get("/:vId", function (req, res) { + const idToFind = Number(req.params.vId); + db.query( + "SELECT * FROM videos where id = $1", + [idToFind], + (error, result) => { + res.status(200).json(result.rows); + } + ); +}); + +//DELETE "/{id}" +app.delete("/:vId", function (req, res) { + const idToDel = Number(req.params.vId); + db.query("DELETE FROM videos WHERE id=$1", [idToDel]) + .then(() => { + res.status(200).send({}); + }) + .catch((e) => + res.status(400).json({ + result: "failure", + message: "Video could not be deleted", + }) + ); +}); + + // Store and retrieve your videos from here // If you want, you can copy "exampleresponse.json" into here to have some data to work with // let videos = [ @@ -86,64 +150,3 @@ const db = new Pool({ // }, // ]; // GET "/" -//test11 -app.get("/", (req, res) => { - - db.query("SELECT * FROM videos", (error, result) => { - console.log(error); - res.json(result.rows); - }); -}); - -// POST "/" -let idCount = 1; -app.post("/", function (req, res) { - const newVideo = req.body; - - const video = { - id: idCount++, - title: newVideo.title, - url: newVideo.url, - }; - - if (!newVideo.title || !newVideo.url) { - res.status(400).json({ - result: "failure", - message: "Video could not be saved", - }); - } else { - const query = - "INSERT INTO videos (id, title, url) " + "VALUES ($1, $2, $3)"; - - db.query(query, [video.id, video.title, video.url], (err, result) => { - res.status(201).json({ id: video.id }); - }); - } -}); - -//GET "/{id}" -app.get("/:vId", function (req, res) { - const idToFind = Number(req.params.vId); - db.query( - "SELECT * FROM videos where id = $1", - [idToFind], - (error, result) => { - res.status(200).json(result.rows); - } - ); -}); - -//DELETE "/{id}" -app.delete("/:vId", function (req, res) { - const idToDel = Number(req.params.vId); - db.query("DELETE FROM videos WHERE id=$1", [idToDel]) - .then(() => { - res.status(200).send({}); - }) - .catch((e) => - res.status(400).json({ - result: "failure", - message: "Video could not be deleted", - }) - ); -});