Skip to content

Atakan Temizkan #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 38 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions src/https.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function httpsServer(app) {
const https = require("https");
const fs = require("fs");

const SSLcert = {
key: fs.readFileSync("/etc/letsencrypt/live/atakan.cloud/privkey.pem"),
cert: fs.readFileSync("/etc/letsencrypt/live/atakan.cloud/fullchain.pem"),
};

const httpsServer = https.createServer(SSLcert, app);
return httpsServer;
}

module.exports = httpsServer;
19 changes: 14 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
/* REQUIRE APP */
const app = require('./server.js')
const port = 3030;
const app = require("./server.js");
const httpsServer = require("./https.js");
// const port = 3030;

/* START SERVER */
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}/`);
httpsServer(app).listen(3030, () => {
console.log(`HTTPS server is running`);
});

app.listen(3031, () => {
console.log(`HTTP server is running`);
});

/* START SERVER */
// app.listen(port, () => {
// console.log(`Server is running on http://localhost:${port}/`);
// });
101 changes: 99 additions & 2 deletions src/routers/books.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,101 @@
// Import data here...
const express = require("express");

const router = express.Router();

// Write routes here...
const { books } = require("../../data/index");

let nextBookId = books.length + 1;

//FUNCTIONS

function getBookById(providedId) {
const foundBook = books.find((book) => book.id == providedId);
return foundBook;
}

function noBookErr(res) {
return res.status(404).json({
error: "A book the provided ID does not exist",
});
}

function missingFieldsErr(res) {
return res.status(400).json({
error: "Missing fields in request body",
});
}

function alreadyExistsErr(res) {
return res.status(409).json({
error: "A book with the provided title already exists",
});
}

//GET ROUTES

router.get("/", (req, res) => {
res.json({ books });
});

router.get("/:id", (req, res) => {
const foundBook = getBookById(req.params.id);

foundBook
? res.json({
book: foundBook,
})
: noBookErr(res);
});

//POST ROUTES

router.post("/", (req, res) => {
if (!req.body.title || !req.body.type || !req.body.author) {
missingFieldsErr(res);
} else if (books.find((book) => book.title == req.body.title)) {
alreadyExistsErr(res);
} else {
const newBook = { id: nextBookId, ...req.body };
books.push(newBook);
res.status(201).json({ book: newBook });
nextBookId++;
}
});

//DELETE ROUTES

router.delete("/:id", (req, res) => {
const foundBook = getBookById(req.params.id);

if (foundBook) {
books.splice(books.indexOf(foundBook), 1);
res.json({
book: foundBook,
});
} else {
noBookErr(res);
}
});

//PUT ROUTES

router.put("/:id", (req, res) => {
const foundBook = getBookById(req.params.id);
if (!req.body.title || !req.body.type || !req.body.author) {
missingFieldsErr(res);
} else if (!foundBook) {
noBookErr(res);
} else if (books.find((book) => book.title == req.body.title)) {
alreadyExistsErr(res);
} else {
let theBook = (books[books.indexOf(foundBook)] = {
id: foundBook.id,
...req.body,
});
res.json({
book: theBook,
});
}
});

module.exports = router;
Loading
Loading