diff --git a/02-nodejs/authenticationServer.js b/02-nodejs/authenticationServer.js index c5278b94..0016d569 100644 --- a/02-nodejs/authenticationServer.js +++ b/02-nodejs/authenticationServer.js @@ -32,6 +32,120 @@ const express = require("express") const PORT = 3000; const app = express(); +const bodyParser = require('body-parser'); +const jwt = require('jsonwebtoken'); +const jwtSecret = 'YOUR_SECRET_STRING_HERE'; + +app.use(bodyParser.json()); // write your logic here, DONT WRITE app.listen(3000) when you're running tests, the tests will automatically start the server +users = [ { "id": 1, +"username": "spande" , +"password":"spande", +"firstname":"swanand", +"lastname":"pande" + +} ,] +users = [] + +app.post('/signup', (req,res)=> { + +data = req.body +usernamm = data.username +userr = users.find((user)=> user.username===usernamm) +if(userr) +{ + res.send(400).json({"message": "user already exists"}) +} +user = {"id" : users.length + 1 , "username": data.username , "password": data.password , "firstName": data.firstName , "lastName":data.lastName , "email":data.email}; + +users.push(user); + +res.status(201).send('Signup successful'); + + +}) + + +app.post('/login',(req,res)=> { + dataa = req.body + // console.log(dataa) + // console.log(dataa.password) + username = dataa.username + password = dataa.password + + // console.log(password); + + userr = users.find((user)=> user.username===username) + if(userr) + { + if(userr.password ==password) + { + res.status(200).json({ + firstName: userr.firstName, + lastName: userr.lastName, + email: userr.email + }); + + } + else + { + res.status(401).send( "Unauthorized") + } + } + else + { + res.status(401).send( "Unauthorized") + } + + + + } + +) +app.get('/data',(req,res)=> { + + username = req.headers.username ; + password = req.headers.password ; + + + userr = users.find((user)=> user.username===username) + + + if(userr) + { + if(userr.password ==password) + { + + userss = users.map((user)=> { + + return [user.username,user.firstname,user.lastname]; + }) + res.status(200).json({"users": userss}) + + } + else + { + res.status(401).send( "Unauthorized") + } + } + else + { + res.status(401).send( "Unauthorized") + } + + + + + + +}) +app.get('*', function(req, res){ + res.status(404).send("invalid url"); +}); + + // app.listen(port, () => { + // console.log(`Example app listening on port ${port}`) + // }) + module.exports = app; diff --git a/02-nodejs/fileServer.js b/02-nodejs/fileServer.js index 82e7719b..1650051b 100644 --- a/02-nodejs/fileServer.js +++ b/02-nodejs/fileServer.js @@ -21,5 +21,65 @@ const fs = require('fs'); const path = require('path'); const app = express(); +const port = 3000; + +// files = [ {"filename": "1"}, +// {"filename":"2"} , + +// {"filename":"3"} +// ] +app.get('/files' , (req,res) => { + +// const filenames = files.map((file)=> { + +// return { "name" : file.filename} ; + +// }) + +fs.readdir('./files', (err, files) => { + if (err) { + // log the error for debugging purposes + console.error("Error reading directory:", err); + + // send a generic server error message to the client + return res.status(500).json({ "message": "Server error" }); + } + + res.status(200).json(files); + + + + + + + +}) +}) + + + app.get("/file/:filename", (req, res) => { + const filename = req.params.filename; + + fs.readFile(`./files/${filename}`, 'utf8', function (err, data) { + if (err) { + // File not found + return res.status(404).send("File not found"); + } + res.status(200).send(data); + } + ); + + }); + app.all('*', (req, res) => { + res.status(404).send('Route not found'); + }); + + // app.listen(port, () => { + // console.log(`Example app listening on port ${port}`) + // }) + + module.exports = app; + + diff --git a/02-nodejs/package-lock.json b/02-nodejs/package-lock.json index b13088b7..f6962ceb 100644 --- a/02-nodejs/package-lock.json +++ b/02-nodejs/package-lock.json @@ -10,6 +10,8 @@ "license": "ISC", "dependencies": { "express": "^4.18.2", + "json": "^11.0.0", + "jsonwebtoken": "^9.0.2", "uuid": "^9.0.0" }, "devDependencies": { @@ -1377,6 +1379,11 @@ "node-int64": "^0.4.0" } }, + "node_modules/buffer-equal-constant-time": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", + "integrity": "sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==" + }, "node_modules/buffer-from": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", @@ -1703,6 +1710,14 @@ "node": "^14.15.0 || ^16.10.0 || >=18.0.0" } }, + "node_modules/ecdsa-sig-formatter": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz", + "integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==", + "dependencies": { + "safe-buffer": "^5.0.1" + } + }, "node_modules/ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -3037,6 +3052,17 @@ "node": ">=4" } }, + "node_modules/json": { + "version": "11.0.0", + "resolved": "https://registry.npmjs.org/json/-/json-11.0.0.tgz", + "integrity": "sha512-N/ITv3Yw9Za8cGxuQqSqrq6RHnlaHWZkAFavcfpH/R52522c26EbihMxnY7A1chxfXJ4d+cEFIsyTgfi9GihrA==", + "bin": { + "json": "lib/json.js" + }, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/json-parse-even-better-errors": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/json-parse-even-better-errors/-/json-parse-even-better-errors-2.3.1.tgz", @@ -3055,6 +3081,76 @@ "node": ">=6" } }, + "node_modules/jsonwebtoken": { + "version": "9.0.2", + "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-9.0.2.tgz", + "integrity": "sha512-PRp66vJ865SSqOlgqS8hujT5U4AOgMfhrwYIuIhfKaoSCZcirrmASQr8CX7cUg+RMih+hgznrjp99o+W4pJLHQ==", + "dependencies": { + "jws": "^3.2.2", + "lodash.includes": "^4.3.0", + "lodash.isboolean": "^3.0.3", + "lodash.isinteger": "^4.0.4", + "lodash.isnumber": "^3.0.3", + "lodash.isplainobject": "^4.0.6", + "lodash.isstring": "^4.0.1", + "lodash.once": "^4.0.0", + "ms": "^2.1.1", + "semver": "^7.5.4" + }, + "engines": { + "node": ">=12", + "npm": ">=6" + } + }, + "node_modules/jsonwebtoken/node_modules/lru-cache": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", + "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", + "dependencies": { + "yallist": "^4.0.0" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/jsonwebtoken/node_modules/semver": { + "version": "7.5.4", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.4.tgz", + "integrity": "sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==", + "dependencies": { + "lru-cache": "^6.0.0" + }, + "bin": { + "semver": "bin/semver.js" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/jsonwebtoken/node_modules/yallist": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" + }, + "node_modules/jwa": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz", + "integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==", + "dependencies": { + "buffer-equal-constant-time": "1.0.1", + "ecdsa-sig-formatter": "1.0.11", + "safe-buffer": "^5.0.1" + } + }, + "node_modules/jws": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz", + "integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==", + "dependencies": { + "jwa": "^1.4.1", + "safe-buffer": "^5.0.1" + } + }, "node_modules/kleur": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/kleur/-/kleur-3.0.3.tgz", @@ -3091,6 +3187,41 @@ "node": ">=8" } }, + "node_modules/lodash.includes": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz", + "integrity": "sha512-W3Bx6mdkRTGtlJISOvVD/lbqjTlPPUDTMnlXZFnVwi9NKJ6tiAk6LVdlhZMm17VZisqhKcgzpO5Wz91PCt5b0w==" + }, + "node_modules/lodash.isboolean": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz", + "integrity": "sha512-Bz5mupy2SVbPHURB98VAcw+aHh4vRV5IPNhILUCsOzRmsTmSQ17jIuqopAentWoehktxGd9e/hbIXq980/1QJg==" + }, + "node_modules/lodash.isinteger": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz", + "integrity": "sha512-DBwtEWN2caHQ9/imiNeEA5ys1JoRtRfY3d7V9wkqtbycnAmTvRRmbHKDV4a0EYc678/dia0jrte4tjYwVBaZUA==" + }, + "node_modules/lodash.isnumber": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz", + "integrity": "sha512-QYqzpfwO3/CWf3XP+Z+tkQsfaLL/EnUlXWVkIk5FUPc4sBdTehEqZONuyRt2P67PXAk+NXmTBcc97zw9t1FQrw==" + }, + "node_modules/lodash.isplainobject": { + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz", + "integrity": "sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA==" + }, + "node_modules/lodash.isstring": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz", + "integrity": "sha512-0wJxfxH1wgO3GrbuP+dTTk7op+6L41QCXbGINEmD+ny/G/eCqGzxyCsh7159S+mgDDcoarnBw6PC1PS5+wUGgw==" + }, + "node_modules/lodash.once": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz", + "integrity": "sha512-Sb487aTOCr9drQVL8pIxOzVhafOjZN9UU54hiN8PU3uAiSV7lx1yYNpbNmex2PK6dSJoNTSJUUswT651yww3Mg==" + }, "node_modules/lru-cache": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz", @@ -3219,8 +3350,7 @@ "node_modules/ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", - "dev": true + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, "node_modules/natural-compare": { "version": "1.4.0", @@ -5351,6 +5481,11 @@ "node-int64": "^0.4.0" } }, + "buffer-equal-constant-time": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", + "integrity": "sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==" + }, "buffer-from": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", @@ -5581,6 +5716,14 @@ "integrity": "sha512-ofrBgwpPhCD85kMKtE9RYFFq6OC1A89oW2vvgWZNCwxrUpRUILopY7lsYyMDSjc8g6U6aiO0Qubg6r4Wgt5ZnA==", "dev": true }, + "ecdsa-sig-formatter": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz", + "integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==", + "requires": { + "safe-buffer": "^5.0.1" + } + }, "ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -6595,6 +6738,11 @@ "integrity": "sha512-OYu7XEzjkCQ3C5Ps3QIZsQfNpqoJyZZA99wd9aWd05NCtC5pWOkShK2mkL6HXQR6/Cy2lbNdPlZBpuQHXE63gA==", "dev": true }, + "json": { + "version": "11.0.0", + "resolved": "https://registry.npmjs.org/json/-/json-11.0.0.tgz", + "integrity": "sha512-N/ITv3Yw9Za8cGxuQqSqrq6RHnlaHWZkAFavcfpH/R52522c26EbihMxnY7A1chxfXJ4d+cEFIsyTgfi9GihrA==" + }, "json-parse-even-better-errors": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/json-parse-even-better-errors/-/json-parse-even-better-errors-2.3.1.tgz", @@ -6607,6 +6755,65 @@ "integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==", "dev": true }, + "jsonwebtoken": { + "version": "9.0.2", + "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-9.0.2.tgz", + "integrity": "sha512-PRp66vJ865SSqOlgqS8hujT5U4AOgMfhrwYIuIhfKaoSCZcirrmASQr8CX7cUg+RMih+hgznrjp99o+W4pJLHQ==", + "requires": { + "jws": "^3.2.2", + "lodash.includes": "^4.3.0", + "lodash.isboolean": "^3.0.3", + "lodash.isinteger": "^4.0.4", + "lodash.isnumber": "^3.0.3", + "lodash.isplainobject": "^4.0.6", + "lodash.isstring": "^4.0.1", + "lodash.once": "^4.0.0", + "ms": "^2.1.1", + "semver": "^7.5.4" + }, + "dependencies": { + "lru-cache": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", + "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", + "requires": { + "yallist": "^4.0.0" + } + }, + "semver": { + "version": "7.5.4", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.4.tgz", + "integrity": "sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==", + "requires": { + "lru-cache": "^6.0.0" + } + }, + "yallist": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" + } + } + }, + "jwa": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz", + "integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==", + "requires": { + "buffer-equal-constant-time": "1.0.1", + "ecdsa-sig-formatter": "1.0.11", + "safe-buffer": "^5.0.1" + } + }, + "jws": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz", + "integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==", + "requires": { + "jwa": "^1.4.1", + "safe-buffer": "^5.0.1" + } + }, "kleur": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/kleur/-/kleur-3.0.3.tgz", @@ -6634,6 +6841,41 @@ "p-locate": "^4.1.0" } }, + "lodash.includes": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz", + "integrity": "sha512-W3Bx6mdkRTGtlJISOvVD/lbqjTlPPUDTMnlXZFnVwi9NKJ6tiAk6LVdlhZMm17VZisqhKcgzpO5Wz91PCt5b0w==" + }, + "lodash.isboolean": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz", + "integrity": "sha512-Bz5mupy2SVbPHURB98VAcw+aHh4vRV5IPNhILUCsOzRmsTmSQ17jIuqopAentWoehktxGd9e/hbIXq980/1QJg==" + }, + "lodash.isinteger": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz", + "integrity": "sha512-DBwtEWN2caHQ9/imiNeEA5ys1JoRtRfY3d7V9wkqtbycnAmTvRRmbHKDV4a0EYc678/dia0jrte4tjYwVBaZUA==" + }, + "lodash.isnumber": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz", + "integrity": "sha512-QYqzpfwO3/CWf3XP+Z+tkQsfaLL/EnUlXWVkIk5FUPc4sBdTehEqZONuyRt2P67PXAk+NXmTBcc97zw9t1FQrw==" + }, + "lodash.isplainobject": { + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz", + "integrity": "sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA==" + }, + "lodash.isstring": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz", + "integrity": "sha512-0wJxfxH1wgO3GrbuP+dTTk7op+6L41QCXbGINEmD+ny/G/eCqGzxyCsh7159S+mgDDcoarnBw6PC1PS5+wUGgw==" + }, + "lodash.once": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz", + "integrity": "sha512-Sb487aTOCr9drQVL8pIxOzVhafOjZN9UU54hiN8PU3uAiSV7lx1yYNpbNmex2PK6dSJoNTSJUUswT651yww3Mg==" + }, "lru-cache": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz", @@ -6729,8 +6971,7 @@ "ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", - "dev": true + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, "natural-compare": { "version": "1.4.0", diff --git a/02-nodejs/package.json b/02-nodejs/package.json index 4858530a..bfba3f99 100644 --- a/02-nodejs/package.json +++ b/02-nodejs/package.json @@ -22,6 +22,8 @@ }, "dependencies": { "express": "^4.18.2", + "json": "^11.0.0", + "jsonwebtoken": "^9.0.2", "uuid": "^9.0.0" } } diff --git a/02-nodejs/todoServer.js b/02-nodejs/todoServer.js index cffc7d60..43b78e73 100644 --- a/02-nodejs/todoServer.js +++ b/02-nodejs/todoServer.js @@ -6,32 +6,32 @@ Each todo has a title and a description. The title is a string and the description is a string. Each todo should also get an unique autogenerated id every time it is created The expected API endpoints are defined below, - 1.GET /todos - Retrieve all todo items - Description: Returns a list of all todo items. + [Done]1.GET /todos - Retrieve all todo items + description: Returns a list of all todo items. Response: 200 OK with an array of todo items in JSON format. Example: GET http://localhost:3000/todos - 2.GET /todos/:id - Retrieve a specific todo item by ID - Description: Returns a specific todo item identified by its ID. + [Done]2.GET /todos/:id - Retrieve a specific todo item by ID + description: Returns a specific todo item identified by its ID. Response: 200 OK with the todo item in JSON format if found, or 404 Not Found if not found. Example: GET http://localhost:3000/todos/123 - 3. POST /todos - Create a new todo item - Description: Creates a new todo item. + [Done]3. POST /todos - Create a new todo item + description: Creates a new todo item. Request Body: JSON object representing the todo item. Response: 201 Created with the ID of the created todo item in JSON format. eg: {id: 1} Example: POST http://localhost:3000/todos Request Body: { "title": "Buy groceries", "completed": false, description: "I should buy groceries" } - 4. PUT /todos/:id - Update an existing todo item by ID - Description: Updates an existing todo item identified by its ID. + [Done]4. PUT /todos/:id - Update an existing todo item by ID + description: Updates an existing todo item identified by its ID. Request Body: JSON object representing the updated todo item. Response: 200 OK if the todo item was found and updated, or 404 Not Found if not found. Example: PUT http://localhost:3000/todos/123 Request Body: { "title": "Buy groceries", "completed": true } 5. DELETE /todos/:id - Delete a todo item by ID - Description: Deletes a todo item identified by its ID. + description: Deletes a todo item identified by its ID. Response: 200 OK if the todo item was found and deleted, or 404 Not Found if not found. Example: DELETE http://localhost:3000/todos/123 @@ -42,8 +42,115 @@ const express = require('express'); const bodyParser = require('body-parser'); +port = 3000; + const app = express(); app.use(bodyParser.json()); + + +todos = [ {"id":1, +"title" : "Todo-1", +"completed" : false, + "description": "description-1" +} , { + "id":2, +"title" : "Todo-2", +"completed" : false, +"description": "description-2" +}, +{"id":3, +"title ": "Todo-2", +"completed" : false, +"description": "description-2" +}, +{"id":4, +"title" : "Todo-3", +"completed" : false, +"description": "description-3" +} ] + +todos = [] + +app.get('/todos', (req, res) => { + res.status(200).json(todos); +}) + +app.get('/todos/:id', (req, res) => { + const id = req.params.id; + const todo = todos.find(todo => todo.id == id); + + + if(todo){ + res.status(200).json(todo); + } + else{ + res.status(404).json({message: `Todo with id ${id} not found`}); + } + +}) + +app.post('/todos', (req, res) => { + + var todo = req.body; + id_no = todos.length + 1; + todo = {"id" :id_no , "title" : todo.title ,"completed": todo.completed,"description" :todo.description }; + + todos.push(todo); + + res.status(201).json({"id":id_no}); + + }) + +app.put('/todos/:id',(req,res) => { + + const id = req.params.id; + const boddy = req.body ; + + var ok=0; + + + todos = todos.map((todo)=> { + + if(todo.id ==id) + { + todo.title = boddy.title; + todo.completed = boddy.completed; + ok=1; + } + + return todo; + + }) + + console.log(todos) + if(ok==1) + res.status(200).json({"message": " Updated"}); + else + res.status(404).json({"message": " Didnt update"}); + +}) + +app.delete('/todos/:id',(req,res) => { + + + var id = req.params.id; + + if(id > todos.length) + res.status(404).json({"message": " Not found"}); + else + todos = todos.splice(id-1,1); + res.status(200).json({"message": " Deleted"}); +}) + + + + // app.listen(port, () => { + // console.log(`Example app listening on port ${port}`) + // }) + + + module.exports = app; +