-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
34 lines (29 loc) · 905 Bytes
/
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
34
const express = require("express");
const ProductService = require("./ProductService");
const app = express();
const port = 3000;
var bodyParser = require("body-parser");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.get("/products", async (req, res) => {
let products = await ProductService.getProducts();
res.json(products);
});
app.get("/products/:id", async (req, res) => {
let id = req.params.id;
let product = await ProductService.getProductById(id);
if (!product || !product[0]) {
res.status(404).send("Product not found");
} else res.json(product);
});
app.post("/products", (req, res) => {
let product = req.body;
ProductService.addProduct(product);
res.status(201).send(product);
});
app.listen(port, () => {
console.log(`Product app listening on port ${port}`);
});