forked from WalaNour/crud-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (40 loc) · 1.26 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
34
35
36
37
38
39
40
41
42
43
44
45
46
const express = require("express");
const path = require("path");
var cors = require("cors");
const app = express();
var bodyParser = require("body-parser");
const Product = require("./database/product.js");
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Serve only the static files form the dist directory
app.use(express.static("./dist/greenplay"));
// Crud Products //
app.get("/api/products", (req, res) => {
Product.find({}, function (error, result) {
if (error) console.log("this is error ====>", error);
res.send(result);
});
});
app.post("/api/products", (req, res) => {
const newM = new Product(req.body);
newM.save((err, result) => {
res.send({ body: "ok" });
});
});
app.put("/api/products/:id", (req, res) => {
Product.updateOne({ _id: req.params.id }, req.body, function (error, result) {
if (error) console.log("this is error ====>", error);
res.send({ result: "done" });
});
});
app.delete("/api/products/:id", (req, res) => {
Product.deleteOne({ _id: req.params.id }, function (error, result) {
if (error) console.log("this is error ====>", error);
res.send(result);
});
});
// end products crud //
app.listen(process.env.PORT || 3000, () => {
console.log("hahaha");
});