Skip to content
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

Add stamp #2

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
40 changes: 40 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import morgan from "morgan";

const app = express();

const customers = ["Bukola", "Caspar", "Dana", "Olu", "Paul"];

app.get("/customers", function (_req, res) {
res.json(customers);
});

//allow morgan logger to get access to each request before and after our handlers
app.use(morgan("dev"));
//auto-include CORS headers to allow consumption of our content by in-browser js loaded from elsewhere
Expand All @@ -14,6 +20,40 @@ app.use(express.json());
//use the environment variable PORT, or 4000 as a fallback
const PORT = process.env.PORT ?? 4000;

const customersloyalty = [
{ name: "Bukola", customerID: 1, stamps: 2 },
{ name: "Caspar", customerID: 2, stamps: 2 },
{ name: "Dana", customerID: 3, stamps: 2 },
{ name: "Olu", customerID: 4, stamps: 2 },
{ name: "Paul", customerID: 5, stamps: 2 },
];

let nextCustomerID = 6;

app.get("/customerLoyalty", function (_req, res) {
res.json(customersloyalty);
});

app.post("/customerLoyalty", (req, res) => {
const newCustomer = {
name: req.body.name,
customerID: nextCustomerID,
stamps: 0,
};
if (!newCustomer) {
console.error("No new customer name received in POST/");
res.status(400).json({
error: "missing customer name from request body",
});
return;
}
nextCustomerID++;
console.log({ newCustomer });
customersloyalty.push(newCustomer);

res.json({ outcome: "success", message: newCustomer });
});

app.listen(PORT, () => {
console.log(
`Your express app started listening on ${PORT}, at ${new Date()}`
Expand Down