-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
33 lines (26 loc) · 1.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const express = require("express");
const app = express();
const categories = require("./db/categories.json");
const menuItems = require("./db/menuItems.json");
const cors = require("cors");
require("dotenv").config();
const port = process.env.PORT || 4000;
const allowedOrigins = process.env.CORS_ALLOWED_ORIGINS || "[]";
app.use(cors({ origin: JSON.parse(allowedOrigins) }));
app.get("/", (req, res) => res.json({ message: "Hello world. :-)" }));
app.get("/categories", (req, res) => res.json(categories));
app.get("/menu-items", (req, res) => {
const { category } = req.query;
if (!category) {
const error = {
message: "Category field is required",
instruction: "Pass category in query params",
};
res.status(400).json(error);
}
const selectedMenuItems = menuItems.filter((e) => e.category === category);
const selectedCategory = categories.find((e) => e.short_name === category);
res.json({ menu_items: selectedMenuItems, category: selectedCategory });
});
app.get("/health", (req, res) => res.status(200).send("OK"));
app.listen(port, () => console.log("Server start on http://localhost:" + port));