From 8ba0ce19ba200e4fdc3b8777e05d45a081de7d7e Mon Sep 17 00:00:00 2001 From: Kristina Date: Fri, 21 Jul 2023 21:49:20 +0100 Subject: [PATCH 01/10] implementation of get and post request --- package-lock.json | 628 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 4 +- server.js | 31 ++- 3 files changed, 658 insertions(+), 5 deletions(-) create mode 100644 package-lock.json diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 00000000..fb560d8c --- /dev/null +++ b/package-lock.json @@ -0,0 +1,628 @@ +{ + "name": "hello-express", + "version": "0.0.1", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "hello-express", + "version": "0.0.1", + "license": "MIT", + "dependencies": { + "cors": "^2.8.5", + "express": "^4.18.2" + }, + "engines": { + "node": "8.x" + } + }, + "node_modules/accepts": { + "version": "1.3.8", + "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.8.tgz", + "integrity": "sha512-PYAthTa2m2VKxuvSD3DPC/Gy+U+sOA1LAuT8mkmRuvw+NACSaeXEQ+NHcVF7rONl6qcaxV3Uuemwawk+7+SJLw==", + "dependencies": { + "mime-types": "~2.1.34", + "negotiator": "0.6.3" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/array-flatten": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", + "integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==" + }, + "node_modules/body-parser": { + "version": "1.20.1", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", + "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", + "dependencies": { + "bytes": "3.1.2", + "content-type": "~1.0.4", + "debug": "2.6.9", + "depd": "2.0.0", + "destroy": "1.2.0", + "http-errors": "2.0.0", + "iconv-lite": "0.4.24", + "on-finished": "2.4.1", + "qs": "6.11.0", + "raw-body": "2.5.1", + "type-is": "~1.6.18", + "unpipe": "1.0.0" + }, + "engines": { + "node": ">= 0.8", + "npm": "1.2.8000 || >= 1.4.16" + } + }, + "node_modules/bytes": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", + "integrity": "sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg==", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/call-bind": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.2.tgz", + "integrity": "sha512-7O+FbCihrB5WGbFYesctwmTKae6rOiIzmz1icreWJ+0aA7LJfuqhEso2T9ncpcFtzMQtzXf2QGGueWJGTYsqrA==", + "dependencies": { + "function-bind": "^1.1.1", + "get-intrinsic": "^1.0.2" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/content-disposition": { + "version": "0.5.4", + "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.4.tgz", + "integrity": "sha512-FveZTNuGw04cxlAiWbzi6zTAL/lhehaWbTtgluJh4/E95DqMwTmha3KZN1aAWA8cFIhHzMZUvLevkw5Rqk+tSQ==", + "dependencies": { + "safe-buffer": "5.2.1" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/content-type": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.5.tgz", + "integrity": "sha512-nTjqfcBFEipKdXCv4YDQWCfmcLZKm81ldF0pAopTvyrFGVbcR6P/VAAd5G7N+0tTr8QqiU0tFadD6FK4NtJwOA==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/cookie": { + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.5.0.tgz", + "integrity": "sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/cookie-signature": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", + "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==" + }, + "node_modules/cors": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", + "dependencies": { + "object-assign": "^4", + "vary": "^1" + }, + "engines": { + "node": ">= 0.10" + } + }, + "node_modules/debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "dependencies": { + "ms": "2.0.0" + } + }, + "node_modules/depd": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", + "integrity": "sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/destroy": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.2.0.tgz", + "integrity": "sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg==", + "engines": { + "node": ">= 0.8", + "npm": "1.2.8000 || >= 1.4.16" + } + }, + "node_modules/ee-first": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", + "integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==" + }, + "node_modules/encodeurl": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz", + "integrity": "sha512-TPJXq8JqFaVYm2CWmPvnP2Iyo4ZSM7/QKcSmuMLDObfpH5fi7RUGmd/rTDf+rut/saiDiQEeVTNgAmJEdAOx0w==", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/escape-html": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", + "integrity": "sha512-NiSupZ4OeuGwr68lGIeym/ksIZMJodUGOSCZ/FSnTxcrekbvqrgdUxlJOMpijaKZVjAJrWrGs/6Jy8OMuyj9ow==" + }, + "node_modules/etag": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz", + "integrity": "sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/express": { + "version": "4.18.2", + "resolved": "https://registry.npmjs.org/express/-/express-4.18.2.tgz", + "integrity": "sha512-5/PsL6iGPdfQ/lKM1UuielYgv3BUoJfz1aUwU9vHZ+J7gyvwdQXFEBIEIaxeGf0GIcreATNyBExtalisDbuMqQ==", + "dependencies": { + "accepts": "~1.3.8", + "array-flatten": "1.1.1", + "body-parser": "1.20.1", + "content-disposition": "0.5.4", + "content-type": "~1.0.4", + "cookie": "0.5.0", + "cookie-signature": "1.0.6", + "debug": "2.6.9", + "depd": "2.0.0", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "etag": "~1.8.1", + "finalhandler": "1.2.0", + "fresh": "0.5.2", + "http-errors": "2.0.0", + "merge-descriptors": "1.0.1", + "methods": "~1.1.2", + "on-finished": "2.4.1", + "parseurl": "~1.3.3", + "path-to-regexp": "0.1.7", + "proxy-addr": "~2.0.7", + "qs": "6.11.0", + "range-parser": "~1.2.1", + "safe-buffer": "5.2.1", + "send": "0.18.0", + "serve-static": "1.15.0", + "setprototypeof": "1.2.0", + "statuses": "2.0.1", + "type-is": "~1.6.18", + "utils-merge": "1.0.1", + "vary": "~1.1.2" + }, + "engines": { + "node": ">= 0.10.0" + } + }, + "node_modules/finalhandler": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.2.0.tgz", + "integrity": "sha512-5uXcUVftlQMFnWC9qu/svkWv3GTd2PfUhK/3PLkYNAe7FbqJMt3515HaxE6eRL74GdsriiwujiawdaB1BpEISg==", + "dependencies": { + "debug": "2.6.9", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "on-finished": "2.4.1", + "parseurl": "~1.3.3", + "statuses": "2.0.1", + "unpipe": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/forwarded": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", + "integrity": "sha512-buRG0fpBtRHSTCOASe6hD258tEubFoRLb4ZNA6NxMVHNw2gOcwHo9wyablzMzOA5z9xA9L1KNjk/Nt6MT9aYow==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/fresh": { + "version": "0.5.2", + "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", + "integrity": "sha512-zJ2mQYM18rEFOudeV4GShTGIQ7RbzA7ozbU9I/XBpm7kqgMywgmylMwXHxZJmkVoYkna9d2pVXVXPdYTP9ej8Q==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/function-bind": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", + "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==" + }, + "node_modules/get-intrinsic": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.2.1.tgz", + "integrity": "sha512-2DcsyfABl+gVHEfCOaTrWgyt+tb6MSEGmKq+kI5HwLbIYgjgmMcV8KQ41uaKz1xxUcn9tJtgFbQUEVcEbd0FYw==", + "dependencies": { + "function-bind": "^1.1.1", + "has": "^1.0.3", + "has-proto": "^1.0.1", + "has-symbols": "^1.0.3" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/has": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", + "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==", + "dependencies": { + "function-bind": "^1.1.1" + }, + "engines": { + "node": ">= 0.4.0" + } + }, + "node_modules/has-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/has-proto/-/has-proto-1.0.1.tgz", + "integrity": "sha512-7qE+iP+O+bgF9clE5+UoBFzE65mlBiVj3tKCrlNQ0Ogwm0BjpT/gK4SlLYDMybDh5I3TCTKnPPa0oMG7JDYrhg==", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/has-symbols": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.3.tgz", + "integrity": "sha512-l3LCuF6MgDNwTDKkdYGEihYjt5pRPbEg46rtlmnSPlUbgmB8LOIrKJbYYFBSbnPaJexMKtiPO8hmeRjRz2Td+A==", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/http-errors": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.0.tgz", + "integrity": "sha512-FtwrG/euBzaEjYeRqOgly7G0qviiXoJWnvEH2Z1plBdXgbyjv34pHTSb9zoeHMyDy33+DWy5Wt9Wo+TURtOYSQ==", + "dependencies": { + "depd": "2.0.0", + "inherits": "2.0.4", + "setprototypeof": "1.2.0", + "statuses": "2.0.1", + "toidentifier": "1.0.1" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/iconv-lite": { + "version": "0.4.24", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", + "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "dependencies": { + "safer-buffer": ">= 2.1.2 < 3" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + }, + "node_modules/ipaddr.js": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", + "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==", + "engines": { + "node": ">= 0.10" + } + }, + "node_modules/media-typer": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", + "integrity": "sha512-dq+qelQ9akHpcOl/gUVRTxVIOkAJ1wR3QAvb4RsVjS8oVoFjDGTc679wJYmUmknUF5HwMLOgb5O+a3KxfWapPQ==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/merge-descriptors": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", + "integrity": "sha512-cCi6g3/Zr1iqQi6ySbseM1Xvooa98N0w31jzUYrXPX2xqObmFGHJ0tQ5u74H3mVh7wLouTseZyYIq39g8cNp1w==" + }, + "node_modules/methods": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz", + "integrity": "sha512-iclAHeNqNm68zFtnZ0e+1L2yUIdvzNoauKU4WBA3VvH/vPFieF7qfRlwUZU+DA9P9bPXIS90ulxoUoCH23sV2w==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz", + "integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==", + "bin": { + "mime": "cli.js" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==" + }, + "node_modules/negotiator": { + "version": "0.6.3", + "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.3.tgz", + "integrity": "sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/object-inspect": { + "version": "1.12.3", + "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.12.3.tgz", + "integrity": "sha512-geUvdk7c+eizMNUDkRpW1wJwgfOiOeHbxBR/hLXK1aT6zmVSO0jsQcs7fj6MGw89jC/cjGfLcNOrtMYtGqm81g==", + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/on-finished": { + "version": "2.4.1", + "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.4.1.tgz", + "integrity": "sha512-oVlzkg3ENAhCk2zdv7IJwd/QUD4z2RxRwpkcGY8psCVcCYZNq4wYnVWALHM+brtuJjePWiYF/ClmuDr8Ch5+kg==", + "dependencies": { + "ee-first": "1.1.1" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/parseurl": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", + "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/path-to-regexp": { + "version": "0.1.7", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", + "integrity": "sha512-5DFkuoqlv1uYQKxy8omFBeJPQcdoE07Kv2sferDCrAq1ohOU+MSDswDIbnx3YAM60qIOnYa53wBhXW0EbMonrQ==" + }, + "node_modules/proxy-addr": { + "version": "2.0.7", + "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz", + "integrity": "sha512-llQsMLSUDUPT44jdrU/O37qlnifitDP+ZwrmmZcoSKyLKvtZxpyV0n2/bD/N4tBAAZ/gJEdZU7KMraoK1+XYAg==", + "dependencies": { + "forwarded": "0.2.0", + "ipaddr.js": "1.9.1" + }, + "engines": { + "node": ">= 0.10" + } + }, + "node_modules/qs": { + "version": "6.11.0", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz", + "integrity": "sha512-MvjoMCJwEarSbUYk5O+nmoSzSutSsTwF85zcHPQ9OrlFoZOYIjaqBAJIqIXjptyD5vThxGq52Xu/MaJzRkIk4Q==", + "dependencies": { + "side-channel": "^1.0.4" + }, + "engines": { + "node": ">=0.6" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/range-parser": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", + "integrity": "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/raw-body": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", + "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", + "dependencies": { + "bytes": "3.1.2", + "http-errors": "2.0.0", + "iconv-lite": "0.4.24", + "unpipe": "1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, + "node_modules/safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + }, + "node_modules/send": { + "version": "0.18.0", + "resolved": "https://registry.npmjs.org/send/-/send-0.18.0.tgz", + "integrity": "sha512-qqWzuOjSFOuqPjFe4NOsMLafToQQwBSOEpS+FwEt3A2V3vKubTquT3vmLTQpFgMXp8AlFWFuP1qKaJZOtPpVXg==", + "dependencies": { + "debug": "2.6.9", + "depd": "2.0.0", + "destroy": "1.2.0", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "etag": "~1.8.1", + "fresh": "0.5.2", + "http-errors": "2.0.0", + "mime": "1.6.0", + "ms": "2.1.3", + "on-finished": "2.4.1", + "range-parser": "~1.2.1", + "statuses": "2.0.1" + }, + "engines": { + "node": ">= 0.8.0" + } + }, + "node_modules/send/node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" + }, + "node_modules/serve-static": { + "version": "1.15.0", + "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.15.0.tgz", + "integrity": "sha512-XGuRDNjXUijsUL0vl6nSD7cwURuzEgglbOaFuZM9g3kwDXOWVTck0jLzjPzGD+TazWbboZYu52/9/XPdUgne9g==", + "dependencies": { + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "parseurl": "~1.3.3", + "send": "0.18.0" + }, + "engines": { + "node": ">= 0.8.0" + } + }, + "node_modules/setprototypeof": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz", + "integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw==" + }, + "node_modules/side-channel": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.0.4.tgz", + "integrity": "sha512-q5XPytqFEIKHkGdiMIrY10mvLRvnQh42/+GoBlFW3b2LXLE2xxJpZFdm94we0BaoV3RwJyGqg5wS7epxTv0Zvw==", + "dependencies": { + "call-bind": "^1.0.0", + "get-intrinsic": "^1.0.2", + "object-inspect": "^1.9.0" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/statuses": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.1.tgz", + "integrity": "sha512-RwNA9Z/7PrK06rYLIzFMlaF+l73iwpzsqRIFgbMLbTcLD6cOao82TaWefPXQvB2fOC4AjuYSEndS7N/mTCbkdQ==", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/toidentifier": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.1.tgz", + "integrity": "sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA==", + "engines": { + "node": ">=0.6" + } + }, + "node_modules/type-is": { + "version": "1.6.18", + "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", + "integrity": "sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==", + "dependencies": { + "media-typer": "0.3.0", + "mime-types": "~2.1.24" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/unpipe": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", + "integrity": "sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ==", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/utils-merge": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", + "integrity": "sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA==", + "engines": { + "node": ">= 0.4.0" + } + }, + "node_modules/vary": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", + "integrity": "sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==", + "engines": { + "node": ">= 0.8" + } + } + } +} diff --git a/package.json b/package.json index f0eeb4b1..d87d3af5 100644 --- a/package.json +++ b/package.json @@ -10,8 +10,8 @@ "start": "node server.js" }, "dependencies": { - "express": "^4.16.4", - "cors": "^2.8.5" + "cors": "^2.8.5", + "express": "^4.18.2" }, "engines": { "node": "8.x" diff --git a/server.js b/server.js index 9e38fadd..dfa41301 100644 --- a/server.js +++ b/server.js @@ -14,10 +14,35 @@ const welcomeMessage = { //This array is our "data store". //We will start with one message in the array. //Note: messages will be lost when Glitch restarts our server. -const messages = [welcomeMessage]; +// const messages = [welcomeMessage]; -app.get("/", function (request, response) { - response.sendFile(__dirname + "/index.html"); +app.get("/", function (req, res) { + console.log("Server is up"); + res.sendFile(__dirname + "/index.html"); +}); + +app.get("/messages", (req, res) => { + console.log(); + res.status(200).send(welcomeMessage); +}); + +app.post("/messages", (req, res) => { + const { from: from, text: text } = req.body; + + if (form && text) { + welcomeMessage.push({ + id: welcomeMessage.length + 1, + from: from, + text: text, + }); + res.status(201).redirect("/"); + } else if (!form) { + res.status(400).send("You missing your name"); + } else if (!text) { + res.status(404).send("You missing your message"); + } else if (!form && !text) { + res.status(400).send("Your missing everything"); + } }); app.listen(process.env.PORT); From 994585daf4e5680cec17991435e2a401e7b6cd68 Mon Sep 17 00:00:00 2001 From: Kristina Date: Sat, 29 Jul 2023 11:15:55 +0100 Subject: [PATCH 02/10] + --- GRADING.md | 19 - HOW-TO-GET-HELP.md | 37 -- HOW-TO-SUBMIT.md | 23 -- HOW_TO_MARK.md | 64 ---- README.md | 24 +- data.json | 1 + package-lock.json | 438 ++++++++++++++++++++++- package.json | 12 +- index.html => public/index.html | 0 screenshots/example_react_quotes_app.png | 0 screenshots/quote-server-example.png | 0 server.js | 137 +++++-- 12 files changed, 569 insertions(+), 186 deletions(-) delete mode 100644 GRADING.md delete mode 100644 HOW-TO-GET-HELP.md delete mode 100644 HOW-TO-SUBMIT.md delete mode 100644 HOW_TO_MARK.md create mode 100644 data.json rename index.html => public/index.html (100%) delete mode 100644 screenshots/example_react_quotes_app.png delete mode 100644 screenshots/quote-server-example.png diff --git a/GRADING.md b/GRADING.md deleted file mode 100644 index b8ab587d..00000000 --- a/GRADING.md +++ /dev/null @@ -1,19 +0,0 @@ - - -# Grading - -All coursework is graded using the Marking Guide found on the Syllabus. - -http://syllabus.codeyourfuture.io/guides/marking-guide - -If you have any questions on these guidelines - please ask. - -## Coding Standards - -Your code should follow our Coding Standards or it will be marked poorly. - -https://syllabus.codeyourfuture.io/guides/code-style-guide diff --git a/HOW-TO-GET-HELP.md b/HOW-TO-GET-HELP.md deleted file mode 100644 index 56c70caf..00000000 --- a/HOW-TO-GET-HELP.md +++ /dev/null @@ -1,37 +0,0 @@ - - -# How To Get Help - -When you get stuck it's important that you ask for help at the right time and in the right way - this means you will be able to solve you problem as quickly as possible! - -## Guide - -Please review our guide on the Syllabus for how to get help - -https://syllabus.codeyourfuture.io/guides/escalation-policy/ - -You should complete all of the steps in the order listed to get help - -## Reporting Issues in Coursework - -Is there a problem with this coursework? -Have you noticed a bug? -Does something not make sense? - -Post in the relevent channel on Slack depending on the module you are completing: - -- Git - `cyf-module-git` -- HTML/CSS - `cyf-module-html-css` -- JavaScript Core 1 - `cyf-module-js1` -- JavaScript Core 2 - `cyf-module-js2` -- JavaScript Core 2 - `cyf-module-js3` -- React - `cyf-module-react` -- Node - `cyf-module-node` -- SQL - `cyf-module-sql` -- MongoDB - `cyf-module-mongodb` - -None of these? Post in #cyf-syllabus. diff --git a/HOW-TO-SUBMIT.md b/HOW-TO-SUBMIT.md deleted file mode 100644 index f9430423..00000000 --- a/HOW-TO-SUBMIT.md +++ /dev/null @@ -1,23 +0,0 @@ - - -# How To Submit Your Coursework - -You should use Git & Github to submit your coursework as a pull request. - -You can use the Github Desktop cheatsheet here to help you do this. - -[Github Desktop Cheatsheet](http://syllabus.codeyourfuture.io/git/cheatsheet) - -You can also use this lesson to help you submit your coursework. - -[Git Lesson](http://syllabus.codeyourfuture.io/git/index) - -## Questions & Help - -Not being able to submit your coursework is not an excuse for not doing it. - -If you cannot submit your coursework you **must** message us on Slack to get help. diff --git a/HOW_TO_MARK.md b/HOW_TO_MARK.md deleted file mode 100644 index 99bf0f0f..00000000 --- a/HOW_TO_MARK.md +++ /dev/null @@ -1,64 +0,0 @@ - - -_This file is useful for Volunteers only_ - -## 1) Solutions - -### 1.1) Where to find solutions? - -You can find the solution to this coursework in a repository with the same name as this but with `-Solution` after the name. - -For example, for this repo: - -https://github.com/CodeYourFuture/JavaScript-Core-1-Coursework-Week1 - -The solutions would be found in: - -https://github.com/CodeYourFuture/JavaScript-Core-1-Coursework-Week1-Solution - -**If you do not have access to these repositories** then please contact your City Coordinator to get access to our Github Team. - -### 1.2) Using the Solutions Repo - -In these repositories you will find solutions to each weeks coursework. These solutions are example answers and will not be the exact solution that students give. You should use it to inform your feedback of the coursework. - -Additionally, you will find marking guides in these places - -- The `marking` folder - Used to store multiple guides on marking -- `marking.md` file - Used to store notes on common problems we're trying to address -- `solutions.md` - A file used by students for notes on best practice - -## 2) Before You Start - -### 2.1) Feedback Guide - -A guide for marking coursework can be found here. Please read it before you start. - -https://docs.codeyourfuture.io/teams/education/homework-feedback - -### 2.2) Marking Guide - -Here is a useful resources you can direct students to when you see them have common mistakes - -https://syllabus.codeyourfuture.io/guides/marking-guide - -This guide should be used when you see a student making a common mistake so instead of writing out a reply you can send them to the a good resource. - -For example, if the student is leaving in lots of comments out code you could write - -```txt -Great work so far! - -It's best if you remove code that you're not using, you can read more about this here -https://syllabus.codeyourfuture.io/guides/marking-guide#commented-out-code -``` - -### 3.3) Style Guide - -All code at CYF should follow this Style Guide - -https://syllabus.codeyourfuture.io/guides/code-style-guide/ diff --git a/README.md b/README.md index 3d6fc232..b6106194 100644 --- a/README.md +++ b/README.md @@ -40,12 +40,12 @@ If you want to share your server with other people the easiest way to do this is At this first level, your API must allow a client to: -- [ ] Create a new message -- [ ] Read all messages -- [ ] Read one message specified by an ID -- [ ] Delete a message, by ID +- [x ] Create a new message +- [x ] Read all messages +- [x ] Read one message specified by an ID +- [x ] Delete a message, by ID -* [ ] All message content should be passed as JSON. +* [x ] All message content should be passed as JSON. * [ ] Your routes should match the patterns established in class (RESTful). See the later spoiler section "Correct Routes" if you need the answer. @@ -83,8 +83,8 @@ What to do now: For this level, your server must: -- [ ] _reject_ requests to create messages if the message objects have an empty or missing `text` or `from` property. - - [ ] In this case your server should return a status code of `400`. +- [x ] _reject_ requests to create messages if the message objects have an empty or missing `text` or `from` property. +- [x ] In this case your server should return a status code of `400`. (Advanced note: people don't actually agree on the best status code for this situation.) @@ -98,23 +98,23 @@ This is a big topic for further study. We won't try to cover it in this challeng For this level your API _must_ also allow a client to: -- [ ] Read _only_ messages whose text contains a given substring: `/messages/search?text=express` +- [x ] Read _only_ messages whose text contains a given substring: `/messages/search?text=express` - [ ] Read only the most recent 10 messages: `/messages/latest` ## Level 4 - Optional - add a timestamp, `timeSent` For this level, the server must: -- [ ] store a timestamp in each message object, in a field called `timeSent`. -- [ ] This should be set to the current time when the server first receives the message. This should be a DateTime object, which can be created with `new Date()`. It will NOT be submitted by the client. +- [x ] store a timestamp in each message object, in a field called `timeSent`. +- [x ] This should be set to the current time when the server first receives the message. This should be a DateTime object, which can be created with `new Date()`. It will NOT be submitted by the client. ## Level 5 - Optional - add message _update_ functionality If you want, you can also: -- [ ] add support for the client to be able to _update_ a message's `text` or `from` property. We'll cover this in the next week of the module, but you can research it easily. +- [x ] add support for the client to be able to _update_ a message's `text` or `from` property. We'll cover this in the next week of the module, but you can research it easily. -- [ ] Your server should NOT update the `timeSent` timestamp property during an update, if the client passes it back to you. +- [x ] Your server should NOT update the `timeSent` timestamp property during an update, if the client passes it back to you. ## Challenge: Advanced: Add a React app as a front-end diff --git a/data.json b/data.json new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/data.json @@ -0,0 +1 @@ +[] diff --git a/package-lock.json b/package-lock.json index fb560d8c..a15aa77d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,12 +10,22 @@ "license": "MIT", "dependencies": { "cors": "^2.8.5", - "express": "^4.18.2" + "express": "^4.18.2", + "morgan": "^1.10.0" + }, + "devDependencies": { + "nodemon": "^3.0.1" }, "engines": { "node": "8.x" } }, + "node_modules/abbrev": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", + "integrity": "sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q==", + "dev": true + }, "node_modules/accepts": { "version": "1.3.8", "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.8.tgz", @@ -28,11 +38,55 @@ "node": ">= 0.6" } }, + "node_modules/anymatch": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.3.tgz", + "integrity": "sha512-KMReFUr0B4t+D+OBkjR3KYqvocp2XaSzO55UcB6mgQMd3KbcE+mWTyvVV7D/zsdEbNnV6acZUutkiHQXvTr1Rw==", + "dev": true, + "dependencies": { + "normalize-path": "^3.0.0", + "picomatch": "^2.0.4" + }, + "engines": { + "node": ">= 8" + } + }, "node_modules/array-flatten": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==" }, + "node_modules/balanced-match": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", + "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", + "dev": true + }, + "node_modules/basic-auth": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz", + "integrity": "sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg==", + "dependencies": { + "safe-buffer": "5.1.2" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/basic-auth/node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, + "node_modules/binary-extensions": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz", + "integrity": "sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==", + "dev": true, + "engines": { + "node": ">=8" + } + }, "node_modules/body-parser": { "version": "1.20.1", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", @@ -56,9 +110,31 @@ "npm": "1.2.8000 || >= 1.4.16" } }, + "node_modules/brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, + "dependencies": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "node_modules/braces": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", + "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==", + "dev": true, + "dependencies": { + "fill-range": "^7.0.1" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/bytes": { "version": "3.1.2", - "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", + "resolved":a "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", "integrity": "sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg==", "engines": { "node": ">= 0.8" @@ -76,6 +152,39 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/chokidar": { + "version": "3.5.3", + "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.5.3.tgz", + "integrity": "sha512-Dr3sfKRP6oTcjf2JmUmFJfeVMvXBdegxB0iVQ5eb2V10uFJUCAS8OByZdVAyVb8xXNz3GjjTgj9kLWsZTqE6kw==", + "dev": true, + "funding": [ + { + "type": "individual", + "url": "https://paulmillr.com/funding/" + } + ], + "dependencies": { + "anymatch": "~3.1.2", + "braces": "~3.0.2", + "glob-parent": "~5.1.2", + "is-binary-path": "~2.1.0", + "is-glob": "~4.0.1", + "normalize-path": "~3.0.0", + "readdirp": "~3.6.0" + }, + "engines": { + "node": ">= 8.10.0" + }, + "optionalDependencies": { + "fsevents": "~2.3.2" + } + }, + "node_modules/concat-map": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", + "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==", + "dev": true + }, "node_modules/content-disposition": { "version": "0.5.4", "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.4.tgz", @@ -212,6 +321,18 @@ "node": ">= 0.10.0" } }, + "node_modules/fill-range": { + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", + "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==", + "dev": true, + "dependencies": { + "to-regex-range": "^5.0.1" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/finalhandler": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.2.0.tgz", @@ -245,6 +366,20 @@ "node": ">= 0.6" } }, + "node_modules/fsevents": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", + "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", + "dev": true, + "hasInstallScript": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, "node_modules/function-bind": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", @@ -264,6 +399,18 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/glob-parent": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz", + "integrity": "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==", + "dev": true, + "dependencies": { + "is-glob": "^4.0.1" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/has": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", @@ -275,6 +422,15 @@ "node": ">= 0.4.0" } }, + "node_modules/has-flag": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", + "integrity": "sha512-sKJf1+ceQBr4SMkvQnBDNDtf4TXpVhVGateu0t918bl30FnbE2m4vNLX+VWe/dpjlb+HugGYzW7uQXH98HPEYw==", + "dev": true, + "engines": { + "node": ">=4" + } + }, "node_modules/has-proto": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/has-proto/-/has-proto-1.0.1.tgz", @@ -323,6 +479,12 @@ "node": ">=0.10.0" } }, + "node_modules/ignore-by-default": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/ignore-by-default/-/ignore-by-default-1.0.1.tgz", + "integrity": "sha512-Ius2VYcGNk7T90CppJqcIkS5ooHUZyIQK+ClZfMfMNFEF9VSE73Fq+906u/CWu92x4gzZMWOwfFYckPObzdEbA==", + "dev": true + }, "node_modules/inherits": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", @@ -336,6 +498,60 @@ "node": ">= 0.10" } }, + "node_modules/is-binary-path": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz", + "integrity": "sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==", + "dev": true, + "dependencies": { + "binary-extensions": "^2.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/is-extglob": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz", + "integrity": "sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==", + "dev": true, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/is-glob": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz", + "integrity": "sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==", + "dev": true, + "dependencies": { + "is-extglob": "^2.1.1" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/is-number": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", + "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", + "dev": true, + "engines": { + "node": ">=0.12.0" + } + }, + "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==", + "dev": true, + "dependencies": { + "yallist": "^4.0.0" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", @@ -387,6 +603,44 @@ "node": ">= 0.6" } }, + "node_modules/minimatch": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", + "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", + "dev": true, + "dependencies": { + "brace-expansion": "^1.1.7" + }, + "engines": { + "node": "*" + } + }, + "node_modules/morgan": { + "version": "1.10.0", + "resolved": "https://registry.npmjs.org/morgan/-/morgan-1.10.0.tgz", + "integrity": "sha512-AbegBVI4sh6El+1gNwvD5YIck7nSA36weD7xvIxG4in80j/UoK8AEGaWnnz8v1GxonMCltmlNs5ZKbGvl9b1XQ==", + "dependencies": { + "basic-auth": "~2.0.1", + "debug": "2.6.9", + "depd": "~2.0.0", + "on-finished": "~2.3.0", + "on-headers": "~1.0.2" + }, + "engines": { + "node": ">= 0.8.0" + } + }, + "node_modules/morgan/node_modules/on-finished": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz", + "integrity": "sha512-ikqdkGAAyf/X/gPhXGvfgAytDZtDbr+bkNUJ0N9h5MI/dmdgCs3l6hoHrcUv41sRKew3jIwrp4qQDXiK99Utww==", + "dependencies": { + "ee-first": "1.1.1" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", @@ -400,6 +654,73 @@ "node": ">= 0.6" } }, + "node_modules/nodemon": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-3.0.1.tgz", + "integrity": "sha512-g9AZ7HmkhQkqXkRc20w+ZfQ73cHLbE8hnPbtaFbFtCumZsjyMhKk9LajQ07U5Ux28lvFjZ5X7HvWR1xzU8jHVw==", + "dev": true, + "dependencies": { + "chokidar": "^3.5.2", + "debug": "^3.2.7", + "ignore-by-default": "^1.0.1", + "minimatch": "^3.1.2", + "pstree.remy": "^1.1.8", + "semver": "^7.5.3", + "simple-update-notifier": "^2.0.0", + "supports-color": "^5.5.0", + "touch": "^3.1.0", + "undefsafe": "^2.0.5" + }, + "bin": { + "nodemon": "bin/nodemon.js" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/nodemon" + } + }, + "node_modules/nodemon/node_modules/debug": { + "version": "3.2.7", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.2.7.tgz", + "integrity": "sha512-CFjzYYAi4ThfiQvizrFQevTTXHtnCqWfe7x1AhgEscTz6ZbLbfoLRLPugTQyBth6f8ZERVUSyWHFD/7Wu4t1XQ==", + "dev": true, + "dependencies": { + "ms": "^2.1.1" + } + }, + "node_modules/nodemon/node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "dev": true + }, + "node_modules/nopt": { + "version": "1.0.10", + "resolved": "https://registry.npmjs.org/nopt/-/nopt-1.0.10.tgz", + "integrity": "sha512-NWmpvLSqUrgrAC9HCuxEvb+PSloHpqVu+FqcO4eeF2h5qYRhA7ev6KvelyQAKtegUbC6RypJnlEOhd8vloNKYg==", + "dev": true, + "dependencies": { + "abbrev": "1" + }, + "bin": { + "nopt": "bin/nopt.js" + }, + "engines": { + "node": "*" + } + }, + "node_modules/normalize-path": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", + "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==", + "dev": true, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/object-assign": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", @@ -427,6 +748,14 @@ "node": ">= 0.8" } }, + "node_modules/on-headers": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.2.tgz", + "integrity": "sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA==", + "engines": { + "node": ">= 0.8" + } + }, "node_modules/parseurl": { "version": "1.3.3", "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", @@ -440,6 +769,18 @@ "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", "integrity": "sha512-5DFkuoqlv1uYQKxy8omFBeJPQcdoE07Kv2sferDCrAq1ohOU+MSDswDIbnx3YAM60qIOnYa53wBhXW0EbMonrQ==" }, + "node_modules/picomatch": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz", + "integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==", + "dev": true, + "engines": { + "node": ">=8.6" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + }, "node_modules/proxy-addr": { "version": "2.0.7", "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz", @@ -452,6 +793,12 @@ "node": ">= 0.10" } }, + "node_modules/pstree.remy": { + "version": "1.1.8", + "resolved": "https://registry.npmjs.org/pstree.remy/-/pstree.remy-1.1.8.tgz", + "integrity": "sha512-77DZwxQmxKnu3aR542U+X8FypNzbfJ+C5XQDk3uWjWxn6151aIMGthWYRXTqT1E5oJvg+ljaa2OJi+VfvCOQ8w==", + "dev": true + }, "node_modules/qs": { "version": "6.11.0", "resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz", @@ -488,6 +835,18 @@ "node": ">= 0.8" } }, + "node_modules/readdirp": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz", + "integrity": "sha512-hOS089on8RduqdbhvQ5Z37A0ESjsqz6qnRcffsMU3495FuTdqSm+7bhJ29JvIOsBDEEnan5DPu9t3To9VRlMzA==", + "dev": true, + "dependencies": { + "picomatch": "^2.2.1" + }, + "engines": { + "node": ">=8.10.0" + } + }, "node_modules/safe-buffer": { "version": "5.2.1", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", @@ -512,6 +871,21 @@ "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" }, + "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==", + "dev": true, + "dependencies": { + "lru-cache": "^6.0.0" + }, + "bin": { + "semver": "bin/semver.js" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/send": { "version": "0.18.0", "resolved": "https://registry.npmjs.org/send/-/send-0.18.0.tgz", @@ -572,6 +946,18 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/simple-update-notifier": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/simple-update-notifier/-/simple-update-notifier-2.0.0.tgz", + "integrity": "sha512-a2B9Y0KlNXl9u/vsW6sTIu9vGEpfKu2wRV6l1H3XEas/0gUIzGzBoP/IouTcUQbm9JWZLH3COxyn03TYlFax6w==", + "dev": true, + "dependencies": { + "semver": "^7.5.3" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/statuses": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.1.tgz", @@ -580,6 +966,30 @@ "node": ">= 0.8" } }, + "node_modules/supports-color": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", + "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", + "dev": true, + "dependencies": { + "has-flag": "^3.0.0" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/to-regex-range": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", + "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==", + "dev": true, + "dependencies": { + "is-number": "^7.0.0" + }, + "engines": { + "node": ">=8.0" + } + }, "node_modules/toidentifier": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.1.tgz", @@ -588,6 +998,18 @@ "node": ">=0.6" } }, + "node_modules/touch": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/touch/-/touch-3.1.0.tgz", + "integrity": "sha512-WBx8Uy5TLtOSRtIq+M03/sKDrXCLHxwDcquSP2c43Le03/9serjQBIztjRz6FkJez9D/hleyAXTBGLwwZUw9lA==", + "dev": true, + "dependencies": { + "nopt": "~1.0.10" + }, + "bin": { + "nodetouch": "bin/nodetouch.js" + } + }, "node_modules/type-is": { "version": "1.6.18", "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", @@ -600,6 +1022,12 @@ "node": ">= 0.6" } }, + "node_modules/undefsafe": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/undefsafe/-/undefsafe-2.0.5.tgz", + "integrity": "sha512-WxONCrssBM8TSPRqN5EmsjVrsv4A8X12J4ArBiiayv3DyyG3ZlIg6yysuuSYdZsVz3TKcTg2fd//Ujd4CHV1iA==", + "dev": true + }, "node_modules/unpipe": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", @@ -623,6 +1051,12 @@ "engines": { "node": ">= 0.8" } + }, + "node_modules/yallist": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", + "dev": true } } } diff --git a/package.json b/package.json index d87d3af5..7b4cc664 100644 --- a/package.json +++ b/package.json @@ -7,11 +7,13 @@ "description": "A simple Node app built on Express, instantly up and running.", "main": "server.js", "scripts": { - "start": "node server.js" + "start": "node server.js", + "dev": "nodemon server.js" }, "dependencies": { "cors": "^2.8.5", - "express": "^4.18.2" + "express": "^4.18.2", + "morgan": "^1.10.0" }, "engines": { "node": "8.x" @@ -24,5 +26,9 @@ "node", "glitch", "express" - ] + ], + "author": "", + "devDependencies": { + "nodemon": "^3.0.1" + } } diff --git a/index.html b/public/index.html similarity index 100% rename from index.html rename to public/index.html diff --git a/screenshots/example_react_quotes_app.png b/screenshots/example_react_quotes_app.png deleted file mode 100644 index e69de29b..00000000 diff --git a/screenshots/quote-server-example.png b/screenshots/quote-server-example.png deleted file mode 100644 index e69de29b..00000000 diff --git a/server.js b/server.js index dfa41301..66724832 100644 --- a/server.js +++ b/server.js @@ -1,48 +1,133 @@ +const PORT = 3001; const express = require("express"); const cors = require("cors"); +const morgan = require("morgan"); const app = express(); +const fs = require("fs"); app.use(cors()); +app.use(morgan("dev")); -const welcomeMessage = { - id: 0, - from: "Bart", - text: "Welcome to CYF chat system!", -}; +app.use(express.static("public")); +app.use(express.json()); +app.use(express.urlencoded({ extended: true })); -//This array is our "data store". -//We will start with one message in the array. -//Note: messages will be lost when Glitch restarts our server. -// const messages = [welcomeMessage]; +//helper functions +//get a convertet ti JSON data +function getConvertedDataFromJSON() { + const bitedData = fs.readFileSync("./data.json"); + const dataAsObj = JSON.parse(bitedData); + return dataAsObj; +} + +function writeUpdateDatatoJsonFile() { + const jsonFile = "./data.json"; + const dataAsJson = JSON.stringify(data); + fs.writeFileSync(jsonFile, dataAsJson); +} + +//get a index.html page app.get("/", function (req, res) { console.log("Server is up"); - res.sendFile(__dirname + "/index.html"); + return res.status(200).send(getConvertedDataFromJSON()); }); +//get all data app.get("/messages", (req, res) => { console.log(); - res.status(200).send(welcomeMessage); + res.status(200).send(getConvertedDataFromJSON()); }); +//post a new obj to data.json app.post("/messages", (req, res) => { - const { from: from, text: text } = req.body; - - if (form && text) { - welcomeMessage.push({ - id: welcomeMessage.length + 1, - from: from, - text: text, - }); - res.status(201).redirect("/"); - } else if (!form) { - res.status(400).send("You missing your name"); - } else if (!text) { - res.status(404).send("You missing your message"); - } else if (!form && !text) { - res.status(400).send("Your missing everything"); + const { from, text } = req.body; + + // Check if 'from' or 'text' properties are empty or missing + if (!from || !text) { + return res + .status(400) + .json({ error: "Both 'from' and 'text' properties are required." }); + } + + const dataAsObj = getConvertedDataFromJSON(); + + dataAsObj.push({ + id: dataAsObj.length, + createAt: new Date(), + updatedAt: new Date(), + from: from, + text: text, + // ...req.body + }); + + writeUpdateDatatoJsonFile(dataAsObj); + return res.status(201).redirect("/"); +}); + +//change spesific obj by ID +app.patch("/messages/:id", (req, res) => { + const requestedID = parseInt(req.params.id); + const newData = req.body; + const dataAsJson = getConvertedDataFromJSON(); + + Object.assign(dataAsJson[requestedID], newData); + dataAsJson[requestedID].updatedAt = new Date(); + writeUpdateDatatoJsonFile(dataAsJson); + + return res.status.redirect("/"); +}); + +//delete spesific obj by ID +app.delete("/messages/:id", (req, res) => { + const requestedID = parseInt(req.params.id); + const dataAsObj = getConvertedDataFromJSON(); + const indexOfObj = dataAsJson.findIndex((obj) => obj.id === requestedID); + + if (indexOfObj >= 0) { + dataAsObj.splice(indexOfObj, 1); + writeUpdateDatatoJsonFile(dataAsObj); + + return res.status(201).redirect("/"); + } + if (indexOfObj < 0) { + return res.status(404).redirect("/"); } }); +//get a specific obj by ID +app.get("/messages/:id", (req, res) => { + const requestedID = parseInt(req.params.id); + const dataAsObj = getConvertedDataFromJSON(); + + const dataByID = dataAsObj.find((element) => element.id === requestedID); + + if (!dataByID) { + res.status(404).send(`Message with ID: ${requestedID} was not found`); + } + writeUpdateDatatoJsonFile(dataAsObj); + res.status(200).redirect("/"); +}); + +//get obj whose text contains a given substring: `/messages/search?text=express` +app.get("/messages/search", (req, res) => { + const searchWord = req.query.input; + const dataAsJson = getConvertedDataFromJSON(); + + const matchedWithInput = dataAsJson.filter((element) => + element.text.toLowerCase().includes(searchWord) + ); + if (!matchedWithInput) { + return res + .status(404) + .send(`We can't find a message which contains: ${searchWord}`); + } + return res.status(200).send(matchedWithInput); +}); + +// app.get("messages/lates", (req, res) => { +// for(let i = 10; i < ) +// }) + app.listen(process.env.PORT); From ae1c7b0193143a6a2df5b4889858393253592e97 Mon Sep 17 00:00:00 2001 From: Kristina Date: Sat, 29 Jul 2023 12:50:47 +0100 Subject: [PATCH 03/10] + --- data.json | 10 +++++++++- server.js | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/data.json b/data.json index fe51488c..50c55960 100644 --- a/data.json +++ b/data.json @@ -1 +1,9 @@ -[] +[ + { + "id": "huhj", + "createAt": "654654654", + "updatedAt": "65464645", + "from": "ihhi", + "text": "huhjuhju" + } +] diff --git a/server.js b/server.js index 66724832..6f6bee2e 100644 --- a/server.js +++ b/server.js @@ -22,7 +22,7 @@ function getConvertedDataFromJSON() { return dataAsObj; } -function writeUpdateDatatoJsonFile() { +function writeUpdateDatatoJsonFile(data) { const jsonFile = "./data.json"; const dataAsJson = JSON.stringify(data); fs.writeFileSync(jsonFile, dataAsJson); From 8c40878d75db7c2485e385f39b02cdb0b304e349 Mon Sep 17 00:00:00 2001 From: Kristina Date: Tue, 1 Aug 2023 21:33:55 +0100 Subject: [PATCH 04/10] + --- server.js | 1 + 1 file changed, 1 insertion(+) diff --git a/server.js b/server.js index 6f6bee2e..1e2c9e23 100644 --- a/server.js +++ b/server.js @@ -43,6 +43,7 @@ app.get("/messages", (req, res) => { //post a new obj to data.json app.post("/messages", (req, res) => { const { from, text } = req.body; + console.log(req.body); // Check if 'from' or 'text' properties are empty or missing if (!from || !text) { From 1ae1c3b8ce3598b34418d9d0a3b499def3d8d845 Mon Sep 17 00:00:00 2001 From: Kristina Date: Tue, 1 Aug 2023 21:44:42 +0100 Subject: [PATCH 05/10] + --- server.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/server.js b/server.js index 1e2c9e23..c0feeb68 100644 --- a/server.js +++ b/server.js @@ -46,11 +46,11 @@ app.post("/messages", (req, res) => { console.log(req.body); // Check if 'from' or 'text' properties are empty or missing - if (!from || !text) { - return res - .status(400) - .json({ error: "Both 'from' and 'text' properties are required." }); - } + // if (!from || !text) { + // return res + // .status(400) + // .json({ error: "Both 'from' and 'text' properties are required." }); + // } const dataAsObj = getConvertedDataFromJSON(); From b1ada0516ab583d8066014235cd5561648c016eb Mon Sep 17 00:00:00 2001 From: Kristina Date: Tue, 1 Aug 2023 21:54:28 +0100 Subject: [PATCH 06/10] + --- data.json | 10 +++++----- server.js | 14 +++++++++----- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/data.json b/data.json index 50c55960..167494fd 100644 --- a/data.json +++ b/data.json @@ -1,9 +1,9 @@ [ { - "id": "huhj", - "createAt": "654654654", - "updatedAt": "65464645", - "from": "ihhi", - "text": "huhjuhju" + "id": 1, + "createdAt": "2023-03-01T12:00:00.000Z", + "updatedAt": "2023-03-02T14:00:00.000Z", + "from": "John Doe", + "text": "Hello World" } ] diff --git a/server.js b/server.js index c0feeb68..37d56387 100644 --- a/server.js +++ b/server.js @@ -15,11 +15,15 @@ app.use(express.urlencoded({ extended: true })); //helper functions //get a convertet ti JSON data -function getConvertedDataFromJSON() { - const bitedData = fs.readFileSync("./data.json"); - const dataAsObj = JSON.parse(bitedData); - - return dataAsObj; +async function getConvertedDataFromJSON() { + try { + const bitedData = await fs.readFile("./data.json"); + const dataAsObj = await JSON.parse(bitedData); + + return dataAsObj; + } catch (error) { + console.log("ERROR in getConvertedDataFromJSON", error); + } } function writeUpdateDatatoJsonFile(data) { From 82621a2ad4555034051ae005fea6db7d86ccda81 Mon Sep 17 00:00:00 2001 From: Kristina Date: Tue, 1 Aug 2023 21:58:37 +0100 Subject: [PATCH 07/10] + --- server.js | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/server.js b/server.js index 37d56387..1e2c9e23 100644 --- a/server.js +++ b/server.js @@ -15,15 +15,11 @@ app.use(express.urlencoded({ extended: true })); //helper functions //get a convertet ti JSON data -async function getConvertedDataFromJSON() { - try { - const bitedData = await fs.readFile("./data.json"); - const dataAsObj = await JSON.parse(bitedData); - - return dataAsObj; - } catch (error) { - console.log("ERROR in getConvertedDataFromJSON", error); - } +function getConvertedDataFromJSON() { + const bitedData = fs.readFileSync("./data.json"); + const dataAsObj = JSON.parse(bitedData); + + return dataAsObj; } function writeUpdateDatatoJsonFile(data) { @@ -50,11 +46,11 @@ app.post("/messages", (req, res) => { console.log(req.body); // Check if 'from' or 'text' properties are empty or missing - // if (!from || !text) { - // return res - // .status(400) - // .json({ error: "Both 'from' and 'text' properties are required." }); - // } + if (!from || !text) { + return res + .status(400) + .json({ error: "Both 'from' and 'text' properties are required." }); + } const dataAsObj = getConvertedDataFromJSON(); From afc44ad889929ecf0ec40411d4ba216b70027d24 Mon Sep 17 00:00:00 2001 From: Kristina Date: Tue, 1 Aug 2023 22:18:25 +0100 Subject: [PATCH 08/10] + --- server.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server.js b/server.js index 1e2c9e23..a0e70f44 100644 --- a/server.js +++ b/server.js @@ -46,8 +46,8 @@ app.post("/messages", (req, res) => { console.log(req.body); // Check if 'from' or 'text' properties are empty or missing - if (!from || !text) { - return res + if (!from && !text) { + res .status(400) .json({ error: "Both 'from' and 'text' properties are required." }); } From 001174a8db4755c93f39b471c6bb95439b47ed24 Mon Sep 17 00:00:00 2001 From: Kristina Date: Thu, 3 Aug 2023 16:08:35 +0100 Subject: [PATCH 09/10] + --- data.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/data.json b/data.json index 167494fd..5d3ef75e 100644 --- a/data.json +++ b/data.json @@ -1,9 +1,9 @@ [ { - "id": 1, + "id": 0, "createdAt": "2023-03-01T12:00:00.000Z", "updatedAt": "2023-03-02T14:00:00.000Z", - "from": "John Doe", - "text": "Hello World" + "from": "Kristina", + "text": "Hello" } ] From 36ae82bb9e54913e7c77fbed671192a956ba1d41 Mon Sep 17 00:00:00 2001 From: Kristina Date: Thu, 3 Aug 2023 17:26:26 +0100 Subject: [PATCH 10/10] + --- data.json | 10 +---- server.js | 130 ++++++++++++++++++++++++++++-------------------------- 2 files changed, 69 insertions(+), 71 deletions(-) diff --git a/data.json b/data.json index 5d3ef75e..abd15138 100644 --- a/data.json +++ b/data.json @@ -1,9 +1 @@ -[ - { - "id": 0, - "createdAt": "2023-03-01T12:00:00.000Z", - "updatedAt": "2023-03-02T14:00:00.000Z", - "from": "Kristina", - "text": "Hello" - } -] +[{"id":0,"createdAt":"2023-03-01T12:00:00.000Z","updatedAt":"2023-03-02T14:00:00.000Z","from":"Kristina","text":"Hello"}] \ No newline at end of file diff --git a/server.js b/server.js index a0e70f44..b6207f7a 100644 --- a/server.js +++ b/server.js @@ -1,4 +1,5 @@ -const PORT = 3001; +const port = 3001; + const express = require("express"); const cors = require("cors"); const morgan = require("morgan"); @@ -13,37 +14,69 @@ app.use(express.static("public")); app.use(express.json()); app.use(express.urlencoded({ extended: true })); -//helper functions -//get a convertet ti JSON data -function getConvertedDataFromJSON() { - const bitedData = fs.readFileSync("./data.json"); - const dataAsObj = JSON.parse(bitedData); +// helper functions +// get a convertet it JSON data +function getConvertedDataFromJSON() { + const bytedData = fs.readFileSync("./data.json"); + const dataAsObj = JSON.parse(bytedData); return dataAsObj; } -function writeUpdateDatatoJsonFile(data) { +// +function writeUpdateDataToJsonFile(data) { const jsonFile = "./data.json"; const dataAsJson = JSON.stringify(data); fs.writeFileSync(jsonFile, dataAsJson); } -//get a index.html page +// get index.html page app.get("/", function (req, res) { - console.log("Server is up"); - return res.status(200).send(getConvertedDataFromJSON()); + return res.status(200).send("index.html"); }); -//get all data +// get all data app.get("/messages", (req, res) => { - console.log(); - res.status(200).send(getConvertedDataFromJSON()); + return res.status(200).send(getConvertedDataFromJSON()); +}); + +// get obj whose text contains a given substring: `/messages/search?text=express` +app.get("/messages/search", (req, res) => { + const searchWord = req.query.input; + const dataAsJson = getConvertedDataFromJSON(); + + const matchedWithInput = dataAsJson.filter((element) => + element.text.toLowerCase().includes(searchWord) + ); + + if (!matchedWithInput) { + return res + .status(404) + .send(`We can't find a message which contains: ${searchWord}`); + } + + return res.status(200).send(matchedWithInput); +}); + +// get a specific obj by ID +app.get("/messages/:id", (req, res) => { + const requestedID = parseInt(req.params.id); + const dataAsObj = getConvertedDataFromJSON(); + + const dataByID = dataAsObj.find((element) => element.id === requestedID); + + if (!dataByID) { + return res + .status(404) + .send(`Message with ID: ${requestedID} was not found`); + } + + return res.status(200).send(dataByID); }); -//post a new obj to data.json +// post a new obj to data.json app.post("/messages", (req, res) => { const { from, text } = req.body; - console.log(req.body); // Check if 'from' or 'text' properties are empty or missing if (!from && !text) { @@ -63,72 +96,45 @@ app.post("/messages", (req, res) => { // ...req.body }); - writeUpdateDatatoJsonFile(dataAsObj); - return res.status(201).redirect("/"); + writeUpdateDataToJsonFile(dataAsObj); + + return res.status(201).send(dataAsObj); }); -//change spesific obj by ID +// change spesific obj by ID app.patch("/messages/:id", (req, res) => { const requestedID = parseInt(req.params.id); const newData = req.body; - const dataAsJson = getConvertedDataFromJSON(); + const datAsObj = getConvertedDataFromJSON(); - Object.assign(dataAsJson[requestedID], newData); - dataAsJson[requestedID].updatedAt = new Date(); - writeUpdateDatatoJsonFile(dataAsJson); + Object.assign(datAsObj[requestedID], newData); + datAsObj[requestedID].updatedAt = new Date(); + writeUpdateDataToJsonFile(datAsObj); - return res.status.redirect("/"); + return res.status().send(datAsObj); }); -//delete spesific obj by ID +// delete spesific obj by ID app.delete("/messages/:id", (req, res) => { const requestedID = parseInt(req.params.id); const dataAsObj = getConvertedDataFromJSON(); - const indexOfObj = dataAsJson.findIndex((obj) => obj.id === requestedID); + const indexOfObj = dataAsObj.findIndex((obj) => obj.id === requestedID); + console.log(indexOfObj); if (indexOfObj >= 0) { + const deletedMessage = dataAsObj[indexOfObj]; dataAsObj.splice(indexOfObj, 1); - writeUpdateDatatoJsonFile(dataAsObj); - - return res.status(201).redirect("/"); - } - if (indexOfObj < 0) { - return res.status(404).redirect("/"); - } -}); - -//get a specific obj by ID -app.get("/messages/:id", (req, res) => { - const requestedID = parseInt(req.params.id); - const dataAsObj = getConvertedDataFromJSON(); - - const dataByID = dataAsObj.find((element) => element.id === requestedID); - - if (!dataByID) { - res.status(404).send(`Message with ID: ${requestedID} was not found`); + writeUpdateDataToJsonFile(dataAsObj); + return res.status(201).send(deletedMessage); } - writeUpdateDatatoJsonFile(dataAsObj); - res.status(200).redirect("/"); -}); - -//get obj whose text contains a given substring: `/messages/search?text=express` -app.get("/messages/search", (req, res) => { - const searchWord = req.query.input; - const dataAsJson = getConvertedDataFromJSON(); - const matchedWithInput = dataAsJson.filter((element) => - element.text.toLowerCase().includes(searchWord) - ); - if (!matchedWithInput) { + if (indexOfObj < 0) { return res .status(404) - .send(`We can't find a message which contains: ${searchWord}`); + .json({ error: `Message with id ${requestedID} does not exist` }); } - return res.status(200).send(matchedWithInput); }); -// app.get("messages/lates", (req, res) => { -// for(let i = 10; i < ) -// }) - -app.listen(process.env.PORT); +app.listen(port, () => { + console.log(`Server listening on Port ${port}`); +});