{"id":17028399,"url":"https://github.com/gazbert/nodejs-cookbook","last_synced_at":"2025-04-05T20:43:25.064Z","repository":{"id":40731873,"uuid":"198676354","full_name":"gazbert/nodejs-cookbook","owner":"gazbert","description":"A cookbook for getting started with node.js development fast!","archived":false,"fork":false,"pushed_at":"2023-01-05T00:56:20.000Z","size":1499,"stargazers_count":0,"open_issues_count":16,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-11T22:57:26.367Z","etag":null,"topics":["express","learning","mongodb","nodejs"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gazbert.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-07-24T16:56:38.000Z","updated_at":"2020-03-15T20:49:28.000Z","dependencies_parsed_at":"2023-02-03T02:46:36.666Z","dependency_job_id":null,"html_url":"https://github.com/gazbert/nodejs-cookbook","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gazbert%2Fnodejs-cookbook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gazbert%2Fnodejs-cookbook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gazbert%2Fnodejs-cookbook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gazbert%2Fnodejs-cookbook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gazbert","download_url":"https://codeload.github.com/gazbert/nodejs-cookbook/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247399898,"owners_count":20932876,"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","learning","mongodb","nodejs"],"created_at":"2024-10-14T07:54:05.291Z","updated_at":"2025-04-05T20:43:25.032Z","avatar_url":"https://github.com/gazbert.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Node.js Cookbook\n\n## What is this?\nA cookbook for getting started with node.js development fast! It's based on the excellent \n[tutorials](https://www.robinwieruch.de/mongodb-express-node-rest-api/) written by Robin Wieruch.\n\nIf you're a Java developer and want to start developing with node.js, you've come to the right place... maybe. ;-)\n\n## Usage\nThere are 3 versions of the app that can be run:\n\n1. `npm run start-stub-nonmod` - Run Express app with stubbed model and non-modular routes.\n1. `npm run ` - Runs Express app with stubbed model and modular routes. \n1. `npm start` - Runs Express app with MongoDB backed model and modular routes.\n\nCall the endpoints, e.g.\n\n* `http://localhost:3000/users/1`\n* `http://localhost:3000/messages/1`\n\n## How does stuff work?\n### npm versioning\n\nNode Package Manager is used to manage node.js dependencies. A bit like Maven and Gradle.\n\nThe `package.json` contains the project's dependency info.\n\nnpm uses [semantic versioning](https://docs.npmjs.com/about-semantic-versioning) for dependency management:\n\n* ~1.0.4 - only take patch updates.\n* ^1.0.4 - only take minor updates (+ patch updates).\n\nRun `npm install` to install _all_ the dependencies in `package.json` - do after checking out of Git.\n\nRun `npm outdated` then `npm update` and to check if we're up to date.\n\n`npm install express --save` - installs and updates prod deps in `package.json`.\n\n`npm install jest --save` - installs and updates dev deps `package.json`.\n\n### Nodemon\nRuns the app in the background when you're developing, so that changes to the code are reflected immediately.\n\n`npm install nodemon --save-dev`\n\nUpdate `pacakge.json` script:\n```\n  \"scripts\": {\n    \"start\": \"nodemon src/index.js\",\n```\n\n### Babel\nUsed to transpile latest JavaScript into simpler JS for node.js engine to execute. Allows developers to use latest ECMAScript features and run them on node.js.\n\n`npm install @babel/core @babel/node --save-dev`\n\nUpdate your `pacakge.json` scripts start line with:\n```\n\"scripts\": {\n    \"start\": \"nodemon --exec babel-node src/index.js\",\n```\n\nNext, you need to tell which features (presets) of ECMAScript to use:\n\n`npm install @babel/preset-env --save-dev`\n\nCreate a .babelrc file in project root and add:\n```\n{\n  \"presets\": [\n    \"@babel/preset-env\"\n  ]\n}\n```\n\n### Express\nA very popular HTTP server for node.js. Excellent support for creating REST endpoints. Bit like Spring REST Coontrollers.\n\n##### Routes\nEssentially a REST endpoint.\n```\nconst app = express();\n\napp.get('/', (req, res) =\u003e {\n  res.send('Hello World!');\n});\n```\n#### Middleware\nBit like servlet filters in the Java world.\n\n##### Application-level middleware\nCan be 3rd party libs or custom impl. Applied at the application level.\n\nFor example, to allow [CORS](https://www.robinwieruch.de/node-js-express-tutorial/) for entire app (adds CORS header). Instead of adding it for every route, it can be done once at the app level.\n\n`npm install cors`\n\n```\nimport cors from 'cors';\n\nconst app = express();\n\napp.use(cors());\n```\n\nAnother example is well-known middleware for parsing HTTP POST requests into JSON:\n\n`npm install body-parser`\n\n```\nimport bodyParser from 'body-parser';\n\napp.use(bodyParser.json());\napp.use(bodyParser.urlencoded({ extended: true }));\n\n```\n\n##### Router-level middleware\nRuns only for a specific route.\n\nTODO: Include example...\n\n### Environment Variables\nUse dotenv to access them. Store your env vars in a `.env` file in project root folder.\n\n`npm install dotenv`\n\n```\nimport 'dotenv/config';\n\nconsole.log(`Example app listening on port ${process.env.PORT}!`),\n```\n\n### MongoDB\nThe app uses [MongoDB](https://www.mongodb.com/) for the database.\n\n* How to install MongoDB on Debian: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-debian/\n* How to install MongoDB on MacOS: https://www.robinwieruch.de/mongodb-macos-setup/\n\n### Mongoose\nThe app uses [Mongoose](https://mongoosejs.com/) for ORM (Onject Relation Mapping) mapping layer.\n\n`npm install mongoose --save`\n\nIt maps a JavaScript object [model](./src/models/user.js) to a MongoDB schema. The model can then be used by the routes to fetch users, delete messages, etc.\n\n## REST API test tools\n\n### Curl\nUse [curl](https://curl.haxx.se/) on the linux command line. Responses can be piped into Bash [jq](https://linuxhint.com/bash_jq_command/) for formatting.\n\nExample curl requests:\n\n`curl http://localhost:3000/messages | jq; # defaults to GET`\n\n`curl -X DELETE http://localhost:3000/messages/msg_123`\n\n``` json\ncurl -X POST -H \"Content-Type:application/json\" http://localhost:3000/messages -d '{\"text\":\"Hi again, World\"}'\n```\n\n## Postman\nUse [Postman](https://www.getpostman.com/).\n\n## Useful VS Code Extensions\n\n[VS Code](https://code.visualstudio.com/) is a decent free IDE for JavaScript development. Some useful extensions include:\n\n1. DotEnv\n1. ESLint\n1. Prettier\n1. GitLens\n1. Jest vs Jest Runner - which is best?\n1. Docker\n1. TODO Tree\n1. HCL (HashiCorp Language support for Vault config)\n\n## References\n1. https://www.robinwieruch.de/mongodb-express-node-rest-api - a great series of tutorials for getting started with node.js, express, and MongoDB integration.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgazbert%2Fnodejs-cookbook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgazbert%2Fnodejs-cookbook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgazbert%2Fnodejs-cookbook/lists"}