This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user