Skip to content

Commit

Permalink
upload Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
Doris-Siu committed Mar 3, 2024
1 parent bc8b65d commit b5e069d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 61 deletions.
2 changes: 2 additions & 0 deletions server/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.env
8 changes: 8 additions & 0 deletions server/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
125 changes: 64 additions & 61 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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",
})
);
});

0 comments on commit b5e069d

Please sign in to comment.