| { | |
| "name": "Food Waste Assistant Backend (CORS + POST)", | |
| "nodes": [ | |
| { | |
| "parameters": { | |
| "content": "## Robust Backend\n\nThis workflow handles:\n1. **POST** requests (the main logic)\n2. **OPTIONS** requests (for CORS preflight checks)\n\n### Instructions\n1. Import & Activate.\n2. Both Webhook nodes allow CORS.\n3. Copy the URL from **Webhook (POST)** and put it in your app." | |
| }, | |
| "id": "desc", | |
| "name": "Note", | |
| "type": "n8n-nodes-base.stickyNote", | |
| "typeVersion": 1, | |
| "position": [ | |
| 0, | |
| 0 | |
| ] | |
| }, | |
| { | |
| "parameters": { | |
| "httpMethod": "POST", | |
| "path": "webhook", | |
| "options": {} | |
| }, | |
| "id": "wh-post", | |
| "name": "Webhook (POST)", | |
| "type": "n8n-nodes-base.webhook", | |
| "typeVersion": 1, | |
| "position": [ | |
| 300, | |
| 200 | |
| ] | |
| }, | |
| { | |
| "parameters": { | |
| "httpMethod": "OPTIONS", | |
| "path": "webhook", | |
| "options": {} | |
| }, | |
| "id": "wh-options", | |
| "name": "Webhook (OPTIONS)", | |
| "type": "n8n-nodes-base.webhook", | |
| "typeVersion": 1, | |
| "position": [ | |
| 300, | |
| 500 | |
| ] | |
| }, | |
| { | |
| "parameters": { | |
| "dataType": "string", | |
| "value1": "={{ $json.body.action }}", | |
| "rules": { | |
| "rules": [ | |
| { | |
| "value2": "add_item", | |
| "output": 0 | |
| }, | |
| { | |
| "value2": "get_items", | |
| "output": 1 | |
| }, | |
| { | |
| "value2": "suggest_recipes", | |
| "output": 2 | |
| } | |
| ] | |
| } | |
| }, | |
| "id": "switch", | |
| "name": "Switch Action", | |
| "type": "n8n-nodes-base.switch", | |
| "typeVersion": 2, | |
| "position": [ | |
| 500, | |
| 200 | |
| ] | |
| }, | |
| { | |
| "parameters": { | |
| "jsCode": "return [ { json: { success: true, message: \"Item added (Real N8N)\" } } ];" | |
| }, | |
| "id": "mock-add", | |
| "name": "Mock Add", | |
| "type": "n8n-nodes-base.code", | |
| "typeVersion": 2, | |
| "position": [ | |
| 750, | |
| 100 | |
| ] | |
| }, | |
| { | |
| "parameters": { | |
| "jsCode": "return [ { json: { items: [ { \"id\": 1, \"name\": \"N8N Connected Apple\", \"quantity\": \"5\", \"expiryDate\": \"2023-12-10\", \"category\": \"Fruits\" } ] } } ];" | |
| }, | |
| "id": "mock-get", | |
| "name": "Mock Get", | |
| "type": "n8n-nodes-base.code", | |
| "typeVersion": 2, | |
| "position": [ | |
| 750, | |
| 300 | |
| ] | |
| }, | |
| { | |
| "parameters": { | |
| "jsCode": "return [ { json: [ { \"title\": \"N8N Salad\", \"image\": \"https://via.placeholder.com/150\", \"ingredients\": \"Lettuce\" } ] } ];" | |
| }, | |
| "id": "mock-recipes", | |
| "name": "Mock Recipes", | |
| "type": "n8n-nodes-base.code", | |
| "typeVersion": 2, | |
| "position": [ | |
| 750, | |
| 500 | |
| ] | |
| }, | |
| { | |
| "parameters": { | |
| "options": { | |
| "responseHeaders": { | |
| "entries": [ | |
| { | |
| "name": "Access-Control-Allow-Origin", | |
| "value": "*" | |
| }, | |
| { | |
| "name": "Access-Control-Allow-Methods", | |
| "value": "POST, OPTIONS" | |
| }, | |
| { | |
| "name": "Access-Control-Allow-Headers", | |
| "value": "*" | |
| } | |
| ] | |
| } | |
| } | |
| }, | |
| "id": "response-cors", | |
| "name": "Respond to Webhook (CORS)", | |
| "type": "n8n-nodes-base.respondToWebhook", | |
| "typeVersion": 1, | |
| "position": [ | |
| 1100, | |
| 300 | |
| ] | |
| }, | |
| { | |
| "parameters": { | |
| "options": { | |
| "responseHeaders": { | |
| "entries": [ | |
| { | |
| "name": "Access-Control-Allow-Origin", | |
| "value": "*" | |
| }, | |
| { | |
| "name": "Access-Control-Allow-Methods", | |
| "value": "POST, OPTIONS" | |
| }, | |
| { | |
| "name": "Access-Control-Allow-Headers", | |
| "value": "*" | |
| } | |
| ] | |
| } | |
| } | |
| }, | |
| "id": "response-options", | |
| "name": "Respond OPTIONS", | |
| "type": "n8n-nodes-base.respondToWebhook", | |
| "typeVersion": 1, | |
| "position": [ | |
| 600, | |
| 500 | |
| ] | |
| } | |
| ], | |
| "connections": { | |
| "Webhook (POST)": { | |
| "main": [ | |
| [ | |
| { | |
| "node": "Switch Action", | |
| "type": "main", | |
| "index": 0 | |
| } | |
| ] | |
| ] | |
| }, | |
| "Webhook (OPTIONS)": { | |
| "main": [ | |
| [ | |
| { | |
| "node": "Respond OPTIONS", | |
| "type": "main", | |
| "index": 0 | |
| } | |
| ] | |
| ] | |
| }, | |
| "Switch Action": { | |
| "main": [ | |
| [ | |
| { | |
| "node": "Mock Add", | |
| "type": "main", | |
| "index": 0 | |
| } | |
| ], | |
| [ | |
| { | |
| "node": "Mock Get", | |
| "type": "main", | |
| "index": 0 | |
| } | |
| ], | |
| [ | |
| { | |
| "node": "Mock Recipes", | |
| "type": "main", | |
| "index": 0 | |
| } | |
| ] | |
| ] | |
| }, | |
| "Mock Add": { | |
| "main": [ | |
| [ | |
| { | |
| "node": "Respond to Webhook (CORS)", | |
| "type": "main", | |
| "index": 0 | |
| } | |
| ] | |
| ] | |
| }, | |
| "Mock Get": { | |
| "main": [ | |
| [ | |
| { | |
| "node": "Respond to Webhook (CORS)", | |
| "type": "main", | |
| "index": 0 | |
| } | |
| ] | |
| ] | |
| }, | |
| "Mock Recipes": { | |
| "main": [ | |
| [ | |
| { | |
| "node": "Respond to Webhook (CORS)", | |
| "type": "main", | |
| "index": 0 | |
| } | |
| ] | |
| ] | |
| } | |
| } | |
| } |