├── .gitignore ├── package.json ├── README.md └── app.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "foodorderingapi", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "start": "node app.js" 9 | }, 10 | "keywords": [], 11 | "author": "", 12 | "license": "ISC", 13 | "dependencies": { 14 | "body-parser": "^1.19.0", 15 | "express": "^4.17.1" 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Food Ordering API with Node.js 2 | 3 | Demostrating how to build a rest api with node.js 4 | 5 | ## Getting Started 6 | 7 | These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system. 8 | 9 | ### Prerequisites 10 | 11 | What things you need to install the software and how to install them 12 | 13 | * Download [Node](https://nodejs.org/en/) and npm 14 | * `npm install` to install all our node dependencies 15 | 16 | ## Running the server 17 | * run `npm start` to start the server. 18 | 19 | 20 | ## Built With 21 | * [Express](https://expressjs.com/) - Fast, unopinionated, minimalist web framework for Node.js 22 | * [Node.js](https://nodejs.org/en/) - Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.] -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | const express = require("express"); 2 | const app = express(); 3 | const bodyparser = require("body-parser"); 4 | 5 | const port = process.env.PORT || 3200; 6 | 7 | //Middle ware 8 | 9 | app.use(bodyparser.json()); 10 | app.use(bodyparser.urlencoded({ extended: false })); 11 | 12 | const orders = []; 13 | 14 | /** 15 | * creating a New order 16 | */ 17 | 18 | app.post("/new_order", (req, res) => { 19 | const order = req.body; 20 | 21 | if (order.food_name || order.customer_name || order.food_qty) { 22 | orders.push({ 23 | ...order, 24 | id: orders.length + 1, 25 | date: Date.now().toString() 26 | }); 27 | console.log(); 28 | res.status(200).json({ 29 | message: "Order created successfully" 30 | }); 31 | } else { 32 | res.status(401).json({ 33 | message: "Invalid Order creation" 34 | }); 35 | } 36 | }); 37 | 38 | /** 39 | * Getting All orders 40 | */ 41 | 42 | app.get("/get_orders", (req, res) => { 43 | res.status(200).send(orders); 44 | }); 45 | 46 | /** 47 | * Update order 48 | */ 49 | app.patch("/order/:id", (req, res) => { 50 | const order_id = req.params.id; 51 | const order_update = req.body; 52 | for (let order of orders) { 53 | if (order.id == order_id) { 54 | if (order_update.food_name != null || undefined) 55 | order.food_name = order_update.food_name; 56 | if (order_update.food_qty != null || undefined) 57 | order.food_qty = order_update.food_qty; 58 | if (order_update.customer_name != null || undefined) 59 | order.customer_name = order_update.customer_name; 60 | 61 | return res 62 | .status(200) 63 | .json({ message: "Updated Succesfully", data: order }); 64 | } 65 | } 66 | 67 | res.status(404).json({ message: "Invalid Order Id" }); 68 | }); 69 | 70 | /** 71 | * Delete Order 72 | */ 73 | app.delete("/order/:id", (req, res) => { 74 | const order_id = req.params.id; 75 | 76 | for (let order of orders) { 77 | if (order.id == order_id) { 78 | orders.splice(orders.indexOf(order), 1); 79 | 80 | return res.status(200).json({ 81 | message: "Deleted Successfully" 82 | }); 83 | } 84 | } 85 | 86 | res.status(404).json({ message: "Invalid Order Id" }); 87 | }); 88 | 89 | app.listen(port, () => { 90 | console.log(`running at port ${port}`); 91 | }); 92 | --------------------------------------------------------------------------------