This commit is contained in:
Philippe Torrel
2026-07-24 13:31:30 +02:00
parent c118892017
commit e5c2f47063
19 changed files with 1380 additions and 659 deletions

View File

@@ -13,7 +13,7 @@ const app = express();
const products = ProductModel();
const corsOptions = {
origin: 'http://localhost:3000',
origin: ['http://localhost:3000', 'http://127.0.0.1:3000'],
optionsSuccessStatus: 200, // some legacy browsers (IE11, various SmartTVs) choke on 204
};
@@ -22,7 +22,12 @@ products.load();
// Middleware
app.use(cors(corsOptions)); // Fängt alle HTTP-Requests vor den Routenabfragen ab.
app.use(express.json()); // HTTP-Request Body wird als JSON Objekt erwartet und geparsed wird.
app.use(express.json()); // HTTP-Request Body wird als JSON Objekt erwartet und geparsed wird. // app.use(bodyParser.json()) - Modul body-parser war in express 4 notwendig
app.use((req, res, next) => {
console.log(color.yellow('HTTP-Method: '), color.magenta(req.method));
next();
});
// Routes
app.get('/', (req, res) => {
@@ -101,6 +106,34 @@ app.put('/api/products', (req, res) => {
return res.send({ msg: 'Product updated', status: 200, success: true, data: product });
});
// HTTP - Methode - PATCH
// reseten von Produkten
app.patch('/api/products/reset', (req, res) => {
const reset = req.body.reset;
if (!reset) {
return res.status(400).send({ msg: 'Could not reset products', status: 400, success: false });
}
products.reset();
return res.send({ msg: 'Products reseted', status: 200, success: true, data: reset });
});
// HTTP - Methode - PATCH
// speichern von Produkten
app.patch('/api/products/save', (req, res) => {
const save = req.body.save;
if (!save) {
return res.status(400).send({ msg: 'Could not save products', status: 400, success: false });
}
products.save();
return res.send({ msg: 'Products saved', status: 200, success: true, data: save });
});
// HTTP - Methode - DELETE
// CRU(D) - delete
// BREA(D) - delete