{"id":18933842,"url":"https://github.com/mchamoudadev/node-js-crash-course","last_synced_at":"2025-09-05T06:31:29.557Z","repository":{"id":214676336,"uuid":"737088055","full_name":"mchamoudadev/node-js-crash-course","owner":"mchamoudadev","description":null,"archived":false,"fork":false,"pushed_at":"2023-12-29T19:27:40.000Z","size":21,"stargazers_count":3,"open_issues_count":0,"forks_count":4,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-01T13:21:02.123Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mchamoudadev.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-12-29T19:26:43.000Z","updated_at":"2025-04-16T10:35:55.000Z","dependencies_parsed_at":null,"dependency_job_id":"5254b760-b324-48a3-b642-b35228ddea49","html_url":"https://github.com/mchamoudadev/node-js-crash-course","commit_stats":null,"previous_names":["mchamoudadev/node-js-crash-course"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mchamoudadev/node-js-crash-course","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mchamoudadev%2Fnode-js-crash-course","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mchamoudadev%2Fnode-js-crash-course/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mchamoudadev%2Fnode-js-crash-course/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mchamoudadev%2Fnode-js-crash-course/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mchamoudadev","download_url":"https://codeload.github.com/mchamoudadev/node-js-crash-course/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mchamoudadev%2Fnode-js-crash-course/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273722702,"owners_count":25156300,"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","status":"online","status_checked_at":"2025-09-05T02:00:09.113Z","response_time":402,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-11-08T11:56:37.656Z","updated_at":"2025-09-05T06:31:28.406Z","avatar_url":"https://github.com/mchamoudadev.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n### Introduction to Node.js\n\n**What is Node.js?**\n\n- **Definition**: Node.js is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code outside a web browser.\n- **Runtime Environment**: Built on the V8 JavaScript engine (same as in Google Chrome).\n- **Non-Blocking I/O**: Asynchronous, event-driven architecture.\n- **Use Case**: Ideal for building scalable network applications.\n\n\n\n**Key Features of Node.js**\n\n1. **Asynchronous and Event-Driven**: Handles multiple operations simultaneously without blocking the thread.\n2. **Single-Threaded**: Simplifies development by avoiding complexity associated with multi-threaded processes.\n3. **Cross-Platform**: Runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.).\n4. **Large Ecosystem**: Enormous NPM (Node Package Manager) repository with reusable packages.\n5. **Community Support**: Strong community and corporate support.\n\n\n\n**Why Choose Node.js for Building APIs?**\n\n1. **Efficiency in Handling Concurrent Requests**: Ideal for I/O-bound applications (like APIs).\n2. **Fast Processing \u0026 Low Latency**: Quick response times for API requests.\n3. **JSON Support**: Native handling of JSON, a common data interchange format for APIs.\n4. **Unified JavaScript Development Stack**: Use the same language for both server-side and client-side.\n5. **Scalability**: Easy to scale for handling high traffic.\n6. **Robust Technology Stack**: Integrates well with other technologies (e.g., MongoDB for databases, Angular/React for frontend).\n\n### 3. Setting Up Node.js\n\n**Installation**:\n\n- Visit the official [Node.js website](https://nodejs.org/) and download the appropriate version for your operating system.\n- There are two primary versions available:\n  - **LTS (Long Term Support)**: Stable and recommended for most users.\n  - **Current**: Contains the latest features but might not be as stable as LTS.\n- After installation, verify it by opening your terminal or command prompt and running:\n\n  ```bash\n  node -v\n  npm -v\n  ```\n\n  This will display the installed versions of Node.js and npm, respectively.\n\n**Node.js Runtime**:\n\n- Node.js allows you to execute JavaScript outside of a browser.\n- Example: Create a file named `hello.js` and write the following code:\n\n  ```javascript\n  console.log(\"Hello from Node.js!\");\n  ```\n\n  Execute the script using:\n\n  ```bash\n\n  node hello.js\n  ```\n\n**Node REPL (Read-Eval-Print Loop)**:\n\n- Node REPL is an interactive shell for executing JavaScript.\n- Access it by typing `node` in your terminal. Within the REPL, you can type any valid JavaScript expression.\n- To exit the REPL, press `Ctrl + C` twice or type `.exit`.\n\n**npm (Node Package Manager)**:\n\n- npm is the default package manager for JavaScript and Node.js.\n- Every Node.js project has a `package.json` file, which tracks project dependencies and other configurations.\n- Create a new Node.js project by navigating to your desired directory and running:\n\n  ```bash\n  npm init\n  ```\n\n  Follow the prompts to set up your project.\n\n- Install external packages/libraries using:\n\n  ```bash\n  npm install \u003cpackage-name\u003e\n  ```\n\n---\n\nGreat! Moving on to the introduction of Express.js.\n\n---\n\n### 4. Introduction to Express.js\n\n**Why Express.js?**:\n\n- Express simplifies the process of building web applications on top of Node.js.\n- It provides a lightweight framework to set up servers, define routes, and handle requests and responses.\n\n**Setting Up a Basic Express Server**:\n\n1. Install Express using npm:\n\n   ```bash\n   npm install express\n   ```\n\n2. Create a new file, `server.js`, and write the following code:\n\n   ```javascript\n   const express = require(\"express\");\n   const app = express();\n\n   app.get(\"/\", (req, res) =\u003e {\n   \tres.send(\"Hello from Express!\");\n   });\n\n   const PORT = 3000;\n   app.listen(PORT, () =\u003e {\n   \tconsole.log(`Server is running on http://localhost:${PORT}`);\n   });\n   ```\n\n3. Run the server with:\n\n   ```bash\n   node server.js\n   ```\n\n**Handling Routes, Middlewares, and Responses**:\n\n- Routes define the endpoints of your application. For example, `/`, `/about`, `/users`.\n- Middlewares are functions that can access the request and response objects. They can perform tasks like logging, authentication, or modifying the request data.\n- Responses can be in various formats like text, JSON, or HTML. With Express, you can easily send different types of responses using methods like `res.send()`, `res.json()`, or `res.render()`.\n\n---\n\n### Building a Node.js API: From Problem to Solution\n\n**1. Starting Point: Basic Express Server**\n\nSet up a basic Express server:\n\n```javascript\nconst express = require(\"express\");\nconst app = express();\nconst PORT = 3000;\n\n// Middleware to parse JSON requests\napp.use(express.json());\n\n// Sample data\nlet users = [\n\t{ id: 1, name: \"John Doe\" },\n\t{ id: 2, name: \"Jane Smith\" },\n];\n\n// GET endpoint to fetch all users\napp.get(\"/users\", (req, res) =\u003e {\n\tres.json(users);\n});\n\n// POST endpoint to add a new user\napp.post(\"/users\", (req, res) =\u003e {\n\tconst newUser = {\n\t\tid: users.length + 1,\n\t\tname: req.body.name,\n\t};\n\tusers.push(newUser);\n\tres.status(201).json(newUser);\n});\n\napp.listen(PORT, () =\u003e {\n\tconsole.log(`Server running on http://localhost:${PORT}`);\n});\n```\n\n\n---\n\n### Essential Libraries \u0026 Tools for Node.js Development\n\n1. **Nodemon**:\n\n   - **Purpose**: It's a utility that monitors for any changes in your source code and automatically restarts your server, making the development process smoother.\n   - **Usage**:\n     ```bash\n     npm install nodemon --save-dev\n     ```\n     In your `package.json` scripts, replace the start script:\n     ```json\n     \"scripts\": {\n       \"start\": \"nodemon server.js\"\n     }\n     ```\n\n2. **Dotenv**:\n\n   - **Purpose**: Allows you to load environment variables from a `.env` file into `process.env`. This is essential for managing sensitive information like database connection strings, API keys, and more.\n   - **Usage**:\n     ```bash\n     npm install dotenv\n     ```\n     At the top of your main server file (e.g., `server.js`):\n     ```javascript\n     require(\"dotenv\").config();\n     ```\n\nCertainly! `Morgan` is a middleware for logging HTTP requests in your Node.js applications. It's handy for monitoring incoming requests, especially in a development environment.\n\nHere's how to set up and use `Morgan`:\n\n---\n\n### Morgan: HTTP Request Logger for Node.js\n\n**Installation**:\n\nTo begin with, you need to install the `morgan` package:\n\n```bash\nnpm install morgan\n```\n\n**Basic Usage**:\n\n1. First, you need to require `morgan` and integrate it into your Express application:\n\n```javascript\nconst express = require(\"express\");\nconst morgan = require(\"morgan\");\n\nconst app = express();\n\n// Use Morgan middleware with the 'dev' format\napp.use(morgan(\"dev\"));\n```\n\n2. Start your server, and for every HTTP request, you'll see a log entry in your terminal. The `'dev'` format is just one of many predefined formats that `morgan` provides.\n\n**Predefined Formats**:\n\nMorgan comes with several predefined formats that control the output style:\n\n- `'combined'`: Standard Apache combined log output.\n- `'common'`: Standard Apache common log output.\n- `'dev'`: Concise output colored by response status for development use.\n- `'short'`: Shorter than default, also colored by response status.\n- `'tiny'`: The minimal output.\n\n**Example**:\n\nIf you want to use the `'combined'` format, just replace `'dev'` with `'combined'`:\n\n```javascript\napp.use(morgan(\"combined\"));\n```\n\n**Custom Formats**:\n\nYou can also define your custom format. For example, to print just the method and URL:\n\n```javascript\napp.use(morgan(\":method :url\"));\n```\n\n---\n\nWith `Morgan`, you can easily keep an eye on your application's incoming requests, helping in debugging and understanding the usage patterns. In a production environment, you might want to integrate `Morgan` with a more advanced logging system to store logs or stream them to external services.\n\n---\n\n\n### Transitioning from `require` to `import` in Node.js\n\n***Embracing ES6 Modules in Node.js***\n\n1. **Module Systems**: \n   - **Current**: CommonJS (`require`, `module.exports`).\n   - **New Standard**: ES6 Modules (`import`, `export`).\n\n2. **Key Differences**:\n   - **Syntax**: `require('module')` → `import module from 'module'`.\n   - **Loading**: Runtime (CommonJS) vs. Compile-time (ES6).\n\n3. **Enabling ES6 Modules**:\n   - Node.js version ≥12.\n   - Add `\"type\": \"module\"` in `package.json`.\n\n4. **Conversion Essentials**:\n   - Update syntax: `require` to `import`.\n   - Specify file extensions in imports.\n   - Handle interop with CommonJS modules.\n\n5. **Best Practices**:\n   - Maintain consistency in module usage.\n   - Thorough testing post-transition.\n   - Stay updated with Node.js and JavaScript community standards.\n\n6. **Benefits**:\n   - Aligns Node.js with modern JavaScript development.\n   - Future-proofs applications.\n   - Improves interoperability with frontend ecosystems.\n\n### Our folder structure\n\n/my-api\n  /node_modules\n  /models\n  /routes\n  /controllers\n  /middlewares\n  /utils\n  /config\n  /tests\n  .env\n  app.js\n  package.json\n\n## Biulding our blog app\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmchamoudadev%2Fnode-js-crash-course","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmchamoudadev%2Fnode-js-crash-course","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmchamoudadev%2Fnode-js-crash-course/lists"}