{"id":21338941,"url":"https://github.com/ajayrandhawa/nodejs-express-restful-api","last_synced_at":"2025-03-16T02:22:36.200Z","repository":{"id":195299483,"uuid":"692651378","full_name":"ajayrandhawa/Nodejs-Express-Restful-Api","owner":"ajayrandhawa","description":"A comprehensive and beginner-friendly guide to creating RESTful APIs with the popular Node.js framework.","archived":false,"fork":false,"pushed_at":"2023-10-04T16:53:57.000Z","size":906,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-22T15:11:28.741Z","etag":null,"topics":["express-api","nodejs-api","restful-api"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ajayrandhawa.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-09-17T06:38:29.000Z","updated_at":"2023-09-17T09:39:02.000Z","dependencies_parsed_at":"2023-10-04T22:26:23.726Z","dependency_job_id":null,"html_url":"https://github.com/ajayrandhawa/Nodejs-Express-Restful-Api","commit_stats":null,"previous_names":["ajayrandhawa/nodejs-express-restful-api"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajayrandhawa%2FNodejs-Express-Restful-Api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajayrandhawa%2FNodejs-Express-Restful-Api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajayrandhawa%2FNodejs-Express-Restful-Api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajayrandhawa%2FNodejs-Express-Restful-Api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ajayrandhawa","download_url":"https://codeload.github.com/ajayrandhawa/Nodejs-Express-Restful-Api/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243814979,"owners_count":20352077,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["express-api","nodejs-api","restful-api"],"created_at":"2024-11-22T00:41:27.091Z","updated_at":"2025-03-16T02:22:35.965Z","avatar_url":"https://github.com/ajayrandhawa.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Simple Nodejs Express Restful Api\n\nA comprehensive and beginner-friendly guide to creating RESTful APIs with the popular Node.js framework, Express.js. This project provides a step-by-step tutorial, code samples, and best practices for designing and implementing robust APIs. Whether you're a novice or an experienced developer, get started quickly and efficiently with ExpressRESTAPIs and streamline your API development workflow.\n\n\n1. Start project with 'npm init' and enter basic details about project.\n\n2. Create 'index.js' file in folder.\n\n3. Install express modules in folder with command 'npm i express'. you can also install nodemon 'npm i nodemon' for live changes to to run server. \n\n### 1. Create First Route\n\nIn we create simple route for get all products using GET request. \n\n```\nconst express = require('express');\nconst app = express();\n\nconst products = [\n    { id: 1, name: 'Iphone 15', price : '520' },  \n    { id: 2, name: 'Samsung S21', price : '500' },  \n    { id: 3, name: 'Nokia L3', price : '370' },  \n];\n\napp.get('/api/products', (req, res) =\u003e {\n    res.send(products);\n})\n\nconst port = process.env.PORT || 3000;\n\napp.listen(port, () =\u003e console.log(`Listiening on port ${port}....`))\n\n```\n\n### 2. Handle Route Params\n\nWith params we can use addional paramter to request for filter and other uses.\n\n```\napp.get('/api/product/:id', (req, res) =\u003e {\n    const product = products.find(product =\u003e product.id === parseInt(req.params.id));\n    if (!product) return res.status(404).send('The Product with the given ID was not found.');\n    res.send(product);\n});\n\n```\n\n### 3. Handle POST Request to Add Product \n\nUsing POST request to add more product, I using JSON data with POST request.\n\n```\napp.use(express.json());\n\napp.post('/api/products', (req, res) =\u003e {\n    const { error } = validateProduct(req.body); \n    if (error) return res.status(400).send(error.details[0].message);\n  \n    const product = {\n      id: products.length + 1,\n      name: req.body.name,\n      price : req.body.price\n    };\n    products.push(product);\n    res.send(product);\n});\n\n\n// Validate Request Data\n\nfunction validateProduct(product) {\n    const schema = Joi.object({\n      name: Joi.string().min(3).required(),\n      price: Joi.number().min(1).required()\n    });\n  \n    return schema.validate(product);\n}\n\n```\n\n### 4. Handle PUT Request and Update Product With ID\n\nUsing JSON data to update products with specific ID\n\n```\napp.put('/api/product/:id', (req, res) =\u003e {\n    const product = products.find(c =\u003e c.id === parseInt(req.params.id));\n    if (!product) return res.status(404).send('The product with the given ID was not found.');\n  \n    const { error } = validateProduct(req.body); \n    if (error) return res.status(400).send(error.details[0].message);\n    \n    product.name = req.body.name; \n    product.price = req.body.price; \n    res.send(product);\n});\n\n```\n\n### 4. Handle DELETE Request and DELETE Product With ID\n\n\n```\napp.delete('/api/product/:id', (req, res) =\u003e {\n    const product = products.find(c =\u003e c.id === parseInt(req.params.id));\n    if (!product) return res.status(404).send('The Product with the given ID was not found.');\n  \n    const index = products.indexOf(product);\n    products.splice(index, 1);\n    res.send(products);\n});\n\n```\n\n### 5. Middleware \n\nThere are some middleware package","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fajayrandhawa%2Fnodejs-express-restful-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fajayrandhawa%2Fnodejs-express-restful-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fajayrandhawa%2Fnodejs-express-restful-api/lists"}