# Dependency in a Node.js Project

# removing a dependency from node project

<div class="WaaZC" id="bkmrk-to-remove-a-dependen"><div class="RJPOee EIJn2" style="animation: none !important;"><div class="rPeykc" data-hveid="CAYQAQ" data-ved="2ahUKEwipv_uxrduQAxXSUGcHHbpUF10Qo_EKegQIBhAB"><span data-huuid="16402721204245620469">To remove a dependency from a Node.js project, the `npm uninstall` command is utilized. </span><span data-huuid="16402721204245619492">This command effectively removes the package from both the `node_modules` directory and the `package.json` file, ensuring a complete removal. </span></div></div></div><div class="WaaZC" id="bkmrk-steps-to-remove-a-de"><div class="RJPOee EIJn2" style="animation: none !important;"><div class="rPeykc pyPiTc" data-hveid="CAQQAQ" data-ved="2ahUKEwipv_uxrduQAxXSUGcHHbpUF10Qo_EKegQIBBAB"><span data-huuid="16402721204245621634">Steps to remove a dependency: </span></div></div></div><div class="WaaZC" id="bkmrk-identify-the-package"><div class="RJPOee EIJn2" style="animation: none !important;">- <div class="zMgcWd dSKvsb" data-il=""><div data-crb-p=""><div class="xFTqob"><div class="Gur8Ad" style="display: inline;"><span data-huuid="16402721204245618703">**Identify the package name:** </span></div><div class="vM0jzc" style="display: inline;"><span data-huuid="16402721204245621822">Locate the exact name of the package you wish to remove within your project's `package.json` file under the `dependencies` or `devDependencies` sections. </span></div></div></div></div>
- <div class="zMgcWd dSKvsb" data-il=""><div data-crb-p=""><div class="xFTqob"><div class="Gur8Ad" style="display: inline;"><span data-huuid="16402721204245619868">**Execute the uninstall command:** </span></div><div class="vM0jzc" style="display: inline;"><span data-huuid="16402721204245618891">Open your terminal or command prompt and navigate to the root directory of your Node.js project. </span><span data-huuid="16402721204245622010">Then, run the `npm uninstall` command followed by the package name: </span></div></div></div></div>

</div></div><div class="WaaZC" id="bkmrk-code"><div class="RJPOee EIJn2" style="animation: none !important;"><div jsaction="rcuQ6b:npT2md" jscontroller="JegcYe"><div class="ecCNFc"><div class="zYSUYd"><div class="FS7GEb"><div class="dDrxod"><div class="x7ndcb">Code</div><div aria-live="polite" class="WDoJJe">  
</div></div></div></div></div></div></div></div>```
    npm uninstall <package_name>
```

<div class="WaaZC" id="bkmrk-for-production-depen"><div class="RJPOee EIJn2" style="animation: none !important;">- <span data-huuid="18156553998514143749">**For production dependencies:** </span><span data-huuid="18156553998514146228">Use the command as shown above. </span>
- <span data-huuid="18156553998514147090">**For development dependencies:** </span><span data-huuid="18156553998514145473">If the package is a development dependency (listed under `devDependencies` in `package.json`), use the `-D` or `--save-dev` flag: </span>

</div></div><div class="WaaZC" id="bkmrk-code-1"><div class="RJPOee EIJn2" style="animation: none !important;"><div jsaction="rcuQ6b:npT2md" jscontroller="JegcYe"><div class="ecCNFc"><div class="zYSUYd"><div class="FS7GEb"><div class="dDrxod"><div class="x7ndcb">Code</div><div aria-live="polite" class="WDoJJe">  
</div></div></div></div></div></div></div></div>```
        npm uninstall -D <package_name>
        # or
        npm uninstall --save-dev <package_name>
```

<div class="WaaZC" id="bkmrk-for-global-packages%3A"><div class="RJPOee EIJn2" style="animation: none !important;">- <span data-huuid="8581022453264046409">**For global packages:** </span><span data-huuid="8581022453264047100">If the package was installed globally, use the `-g` flag: </span>

</div></div><div class="WaaZC" id="bkmrk-code-2"><div class="RJPOee EIJn2" style="animation: none !important;"><div jsaction="rcuQ6b:npT2md" jscontroller="JegcYe"><div class="ecCNFc"><div class="zYSUYd"><div class="FS7GEb"><div class="dDrxod"><div class="x7ndcb">Code</div><div aria-live="polite" class="WDoJJe">  
</div></div></div></div></div></div></div></div>```
        npm uninstall -g <package_name>
```

<div class="WaaZC" id="bkmrk-you-can-also-uninsta"><div class="RJPOee EIJn2" style="animation: none !important;"><div class="rPeykc" data-hveid="CCgQAQ" data-ved="2ahUKEwipv_uxrduQAxXSUGcHHbpUF10Qo_EKegQIKBAB"><span data-huuid="5458187328726950890">You can also uninstall multiple packages at once by listing them separated by spaces: </span></div></div></div><div class="WaaZC" id="bkmrk-code-3"><div class="RJPOee EIJn2" style="animation: none !important;"><div jsaction="rcuQ6b:npT2md" jscontroller="JegcYe"><div class="ecCNFc"><div class="zYSUYd"><div class="FS7GEb"><div class="dDrxod"><div class="x7ndcb">Code</div><div aria-live="polite" class="WDoJJe">  
</div></div></div></div></div></div></div></div>```
    npm uninstall <package1> <package2>
```

<div class="WaaZC" id="bkmrk-explanation%3A"><div class="RJPOee EIJn2" style="animation: none !important;"><div class="rPeykc pyPiTc" data-hveid="CCwQAQ" data-ved="2ahUKEwipv_uxrduQAxXSUGcHHbpUF10Qo_EKegQILBAB"><span data-huuid="4180915626508920186">Explanation: </span></div></div></div><div class="WaaZC" id="bkmrk-running-npm-uninstal"><div class="RJPOee EIJn2" style="animation: none !important;">- <span data-huuid="4180915626508920839">Running `npm uninstall` removes the package's files from your `node_modules` directory. </span>
- <span data-huuid="4180915626508919909">Simultaneously, it updates your `package.json` file by removing the entry for the uninstalled package from either the `dependencies` or `devDependencies` list, depending on how it was originally installed. </span><span data-huuid="4180915626508921492">This prevents the package from being reinstalled during future `npm install` operations</span>

</div></div>

# Ajv (Another JSON Schema Validator)

<div class="WaaZC" id="bkmrk-ajv-%28another-json-sc"><div class="RJPOee EIJn2" style="animation: none !important;"><div class="rPeykc" data-hveid="CAUQAQ" data-ved="2ahUKEwj8hrT2rduQAxW2WHADHR_0Em4Qo_EKegQIBRAB"><span data-huuid="13877622576156675059">Ajv (Another JSON Schema Validator) is a high-performance JSON schema validator widely used in Node.js applications. </span><span data-huuid="13877622576156675282">It ensures that data conforms to a predefined structure and set of rules, which is crucial for maintaining data integrity and application stability. </span></div></div></div><div class="WaaZC" id="bkmrk-key-features-and-usa"><div class="RJPOee EIJn2" style="animation: none !important;"><div class="rPeykc pyPiTc" data-hveid="CAQQAQ" data-ved="2ahUKEwj8hrT2rduQAxW2WHADHR_0Em4Qo_EKegQIBBAB"><span data-huuid="13877622576156675728">Key Features and Usage in Node.js: </span></div></div></div><div class="WaaZC" id="bkmrk-json-schema-validati"><div class="RJPOee EIJn2" style="animation: none !important;">- <span data-huuid="13877622576156672301">**JSON Schema Validation:** </span><span data-huuid="13877622576156672524">Ajv validates data against JSON Schema specifications (drafts 04, 06, 07, 2019-09, and 2020-12) and JSON Type Definition (JTD). </span><span data-huuid="13877622576156672747">This allows you to define the expected structure, data types, and constraints for your JSON data. </span>
- <span data-huuid="13877622576156673193">**Performance:** </span><span data-huuid="13877622576156673416">Ajv is known for its speed. </span><span data-huuid="13877622576156673639">It compiles schemas into optimized JavaScript functions, which are highly efficient for validating data, especially in performance-critical Node.js environments. </span>
- <span data-huuid="13877622576156674085">**Installation:** </span><span data-huuid="13877622576156674308">You can install Ajv in your Node.js project using npm:<span class="pjBG2e" data-cid="2948a6a2-abf0-4d4d-a64a-c9ceaa6b2957"><span class="UV3uM"> </span></span></span><div class="NPrrbc" data-cid="2948a6a2-abf0-4d4d-a64a-c9ceaa6b2957" data-uuids="13877622576156672301,13877622576156672524,13877622576156672747,13877622576156673193,13877622576156673416,13877622576156673639,13877622576156674085,13877622576156674308"><div aria-label="View related links" class="BMebGe btku5b LwdV0e FR7ZSc OJeuxf" data-hveid="CBAQAQ" data-ved="2ahUKEwj8hrT2rduQAxW2WHADHR_0Em4Q3fYKegQIEBAB" jsaction="KjsqPd" jsname="HtgYJd" role="button" tabindex="0"><div class="niO4u"><span class="d3o3Ad TwMgNb Hkv2Pe"><span class="iPjmzb Sorfoc gNGSDf"><span class="z1asCe Sb7k4e" style="height: 18px; line-height: 18px; width: 18px;"><svg aria-hidden="true" focusable="false" viewbox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"></svg></span></span></span></div></div></div>

</div></div><div class="WaaZC" id="bkmrk-"><div class="RJPOee EIJn2" style="animation: none !important;">  
</div></div><div class="WaaZC" id="bkmrk-code"><div class="RJPOee EIJn2" style="animation: none !important;"><div jsaction="rcuQ6b:npT2md" jscontroller="JegcYe"><div class="ecCNFc"><div class="zYSUYd"><div class="FS7GEb"><div class="dDrxod"><div class="x7ndcb">Code</div><div aria-live="polite" class="WDoJJe">  
</div></div></div></div></div></div></div></div>```
    npm install ajv
```

<div class="WaaZC" id="bkmrk-basic-usage."><div class="RJPOee EIJn2" style="animation: none !important;"><div class="rPeykc" data-hveid="CBsQAQ" data-ved="2ahUKEwj8hrT2rduQAxW2WHADHR_0Em4Qo_EKegQIGxAB"><span data-huuid="2257737679648533967">Basic Usage. </span></div></div></div><div class="WaaZC" id="bkmrk-javascript"><div class="RJPOee EIJn2" style="animation: none !important;"><div jsaction="rcuQ6b:npT2md" jscontroller="JegcYe"><div class="ecCNFc"><div class="zYSUYd"><div class="FS7GEb"><div class="dDrxod"><div class="x7ndcb">JavaScript</div><div aria-live="polite" class="WDoJJe">  
</div></div></div></div></div></div></div></div>```
    const Ajv = require('ajv');
    const ajv = new Ajv(); // Options can be passed here, e.g., { allErrors: true }

    const schema = {
      type: 'object',
      properties: {
        name: { type: 'string' },
        age: { type: 'integer', minimum: 0 }
      },
      required: ['name', 'age'],
      additionalProperties: false
    };

    const validate = ajv.compile(schema);

    const data = { name: 'John Doe', age: 30 };
    const valid = validate(data);

    if (!valid) {
      console.log(validate.errors); // Array of validation errors
    } else {
      console.log('Data is valid');
    }
```

<div class="WaaZC" id="bkmrk-error-handling%3A-when"><div class="RJPOee EIJn2" style="animation: none !important;">- <div class="zMgcWd dSKvsb" data-il=""><div data-crb-p=""><div class="xFTqob"><div class="Gur8Ad" style="display: inline;"><span data-huuid="17470203091346203919">**Error Handling:** </span></div><div class="vM0jzc" style="display: inline;"><span data-huuid="17470203091346202648">When validation fails, `validate.errors` will contain an array of `ErrorObject` detailing the validation issues, which can be used to provide informative feedback to users or for debugging. </span></div></div></div></div>
- <div class="zMgcWd dSKvsb" data-il=""><div data-crb-p=""><div class="xFTqob"><div class="Gur8Ad" style="display: inline;"><span data-huuid="17470203091346204202">**Integration with Frameworks:** </span></div><div class="vM0jzc" style="display: inline;"><span data-huuid="17470203091346202931">Ajv can be integrated with Node.js web frameworks like Express or Koa to validate incoming request bodies, ensuring that API payloads adhere to defined schemas. </span><span data-huuid="17470203091346205756">This helps in preventing invalid data from reaching your application logic. </span></div></div></div></div>
- <div class="zMgcWd dSKvsb" data-il=""><div data-crb-p=""><div class="xFTqob"><div class="Gur8Ad" style="display: inline;"><span data-huuid="17470203091346203214">**Custom Keywords and Formats:** </span></div><div class="vM0jzc" style="display: inline;"><span data-huuid="17470203091346206039">Ajv allows for extending its capabilities with custom keywords and formats, enabling highly specific validation logic tailored to your application's needs. </span><span data-huuid="17470203091346204768">The `ajv-formats` plugin provides support for common formats like `date`, `time`, and `email`</span></div><div class="vM0jzc" style="display: inline;">  
    </div><div class="vM0jzc" style="display: inline;">  
    </div><div class="vM0jzc" style="display: inline;"></div><div class="vM0jzc" style="display: inline;"></div><div class="vM0jzc" style="display: inline;"></div><div class="vM0jzc" style="display: inline;"></div><div class="vM0jzc" style="display: inline;"><span data-huuid="17470203091346204768">https://ajv.js.org/guide/getting-started.html</span></div></div></div></div>

</div></div>

# Steps to use Morgan in a Node.js Express application

<div class="WaaZC" id="bkmrk-organ-is-an-http-req" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div class="rPeykc" data-hveid="CAQQAQ" data-ved="2ahUKEwiktfqwq9uQAxW6SmwGHZkSPFUQo_EKegQIBBAB" style="margin: 0px 0px 10px;"><span data-huuid="16869043901086325898">organ is an HTTP request logger middleware for Node.js, primarily used with Express applications. </span><span data-huuid="16869043901086326181">It provides a way to log details about incoming requests and outgoing responses to the console or a file.</span></div></div></div><div class="WaaZC" id="bkmrk-steps-to-use-morgan-" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div class="rPeykc pyPiTc" data-hveid="CAcQAQ" data-ved="2ahUKEwiktfqwq9uQAxW6SmwGHZkSPFUQo_EKegQIBxAB" style="margin: 20px 0px 10px; font-size: 20px; font-weight: 600; line-height: 28px;"><span data-huuid="16869043901086326747">Steps to use Morgan in a Node.js Express application:</span></div></div></div><div class="WaaZC" id="bkmrk-initialize-a-node.js" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);">- <span data-huuid="16869043901086323500">**Initialize a Node.js project and install necessary packages:**</span>

</div></div><div class="WaaZC" id="bkmrk-code" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div jsaction="rcuQ6b:npT2md" jscontroller="JegcYe"><div class="ecCNFc" style="border-image: none 100% / 1 / 0 stretch; border-radius: 8px; font-size: 14px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5); margin-bottom: 20px; color: rgb(10, 10, 10); border: 1.2px solid rgb(169, 172, 170);"><div class="zYSUYd" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 8px 8px 0px 0px;"><div class="FS7GEb" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 629.381px;"><div class="dDrxod" style="height: 34px; display: flex; justify-content: space-between; align-items: center; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(229, 237, 255); border-bottom: 1.2px solid rgb(169, 172, 170);"><div class="x7ndcb" style="padding-left: 16px; display: flex; align-items: center; color: rgb(10, 10, 10);">Code</div><div aria-live="polite" class="WDoJJe" style="margin-right: 4px; display: flex; align-items: center;"><button aria-label="Copy code to clipboard" class="hqI3tf B4zsNc" data-ved="2ahUKEwiktfqwq9uQAxW6SmwGHZkSPFUQ9MILegQIAhAB" role="button" style="margin: 0px 2px; background: none; height: 34px; border: none; color: rgb(5, 40, 97); position: relative; top: 2px; outline: 0px;" tabindex="0" title="Copy"><span class="z1asCe wm4nBd" style="display: inline-block; fill: currentcolor; height: 20px; line-height: 20px; position: relative; width: 20px;"><svg aria-hidden="true" enable-background="new 0 0 24 24" focusable="false" height="24" viewbox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><g><rect fill="none" height="24" width="24"></rect></g><g><path d="M16,20H5V6H3v14c0,1.1,0.9,2,2,2h11V20z M20,16V4c0-1.1-0.9-2-2-2H9C7.9,2,7,2.9,7,4v12c0,1.1,0.9,2,2,2h9 C19.1,18,20,17.1,20,16z M18,16H9V4h9V16z"></path></g></svg></span></button></div></div></div></div><div class="QQjpRc" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 0px 0px 8px 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5);"><div class="FS7GEb" jsname="zEXxAd" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 629.381px;"></div></div></div></div></div></div>```
    mkdir my-express-app
    cd my-express-app
    npm init -y
    npm install express morgan --save
```

<div class="WaaZC" id="bkmrk-create-your-main-app" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);">- <span data-huuid="3701120719997211110">Create your main application file (e.g., `index.js`):</span>

</div></div><div class="WaaZC" id="bkmrk-javascript" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div jsaction="rcuQ6b:npT2md" jscontroller="JegcYe"><div class="ecCNFc" style="border-image: none 100% / 1 / 0 stretch; border-radius: 8px; font-size: 14px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5); margin-bottom: 20px; color: rgb(10, 10, 10); border: 1.2px solid rgb(169, 172, 170);"><div class="zYSUYd" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 8px 8px 0px 0px;"><div class="FS7GEb" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 629.381px;"><div class="dDrxod" style="height: 34px; display: flex; justify-content: space-between; align-items: center; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(229, 237, 255); border-bottom: 1.2px solid rgb(169, 172, 170);"><div class="x7ndcb" style="padding-left: 16px; display: flex; align-items: center; color: rgb(10, 10, 10);">JavaScript</div><div aria-live="polite" class="WDoJJe" style="margin-right: 4px; display: flex; align-items: center;"><button aria-label="Copy code to clipboard" class="hqI3tf B4zsNc" data-ved="2ahUKEwiktfqwq9uQAxW6SmwGHZkSPFUQ9MILegQIFBAB" role="button" style="margin: 0px 2px; background: none; height: 34px; border: none; color: rgb(5, 40, 97); position: relative; top: 2px; outline: 0px;" tabindex="0" title="Copy"><span class="z1asCe wm4nBd" style="display: inline-block; fill: currentcolor; height: 20px; line-height: 20px; position: relative; width: 20px;"><svg aria-hidden="true" enable-background="new 0 0 24 24" focusable="false" height="24" viewbox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><g><rect fill="none" height="24" width="24"></rect></g><g><path d="M16,20H5V6H3v14c0,1.1,0.9,2,2,2h11V20z M20,16V4c0-1.1-0.9-2-2-2H9C7.9,2,7,2.9,7,4v12c0,1.1,0.9,2,2,2h9 C19.1,18,20,17.1,20,16z M18,16H9V4h9V16z"></path></g></svg></span></button></div></div></div></div><div class="QQjpRc" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 0px 0px 8px 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5);"><div class="FS7GEb" jsname="zEXxAd" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 629.381px;"></div></div></div></div></div></div>```
    // index.js
    const express = require('express');
    const morgan = require('morgan'); // Import morgan
    const app = express();
    const PORT = 3000;

    // Use morgan as middleware
    // You can choose a predefined format like 'dev', 'tiny', 'combined', 'common', 'short'
    app.use(morgan('dev'));

    // Define a simple route
    app.get('/', (req, res) => {
        res.send('Hello from Express!');
    });

    // Start the server
    app.listen(PORT, () => {
        console.log(`Server running on port ${PORT}`);
    });
```

<div class="WaaZC" id="bkmrk-run-your-application" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div class="rPeykc" data-hveid="CBcQAQ" data-ved="2ahUKEwiktfqwq9uQAxW6SmwGHZkSPFUQo_EKegQIFxAB" style="margin: 10px 0px;"><span data-huuid="15255164111608769301">Run your application.</span></div></div></div><div class="WaaZC" id="bkmrk-code-1" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div jsaction="rcuQ6b:npT2md" jscontroller="JegcYe"><div class="ecCNFc" style="border-image: none 100% / 1 / 0 stretch; border-radius: 8px; font-size: 14px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5); margin-bottom: 20px; color: rgb(10, 10, 10); border: 1.2px solid rgb(169, 172, 170);"><div class="zYSUYd" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 8px 8px 0px 0px;"><div class="FS7GEb" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 629.381px;"><div class="dDrxod" style="height: 34px; display: flex; justify-content: space-between; align-items: center; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(229, 237, 255); border-bottom: 1.2px solid rgb(169, 172, 170);"><div class="x7ndcb" style="padding-left: 16px; display: flex; align-items: center; color: rgb(10, 10, 10);">Code</div><div aria-live="polite" class="WDoJJe" style="margin-right: 4px; display: flex; align-items: center;"><button aria-label="Copy code to clipboard" class="hqI3tf B4zsNc" data-ved="2ahUKEwiktfqwq9uQAxW6SmwGHZkSPFUQ9MILegQIGRAB" role="button" style="margin: 0px 2px; background: none; height: 34px; border: none; color: rgb(5, 40, 97); position: relative; top: 2px; outline: 0px;" tabindex="0" title="Copy"><span class="z1asCe wm4nBd" style="display: inline-block; fill: currentcolor; height: 20px; line-height: 20px; position: relative; width: 20px;"><svg aria-hidden="true" enable-background="new 0 0 24 24" focusable="false" height="24" viewbox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><g><rect fill="none" height="24" width="24"></rect></g><g><path d="M16,20H5V6H3v14c0,1.1,0.9,2,2,2h11V20z M20,16V4c0-1.1-0.9-2-2-2H9C7.9,2,7,2.9,7,4v12c0,1.1,0.9,2,2,2h9 C19.1,18,20,17.1,20,16z M18,16H9V4h9V16z"></path></g></svg></span></button></div></div></div></div><div class="QQjpRc" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 0px 0px 8px 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5);"><div class="FS7GEb" jsname="zEXxAd" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 629.381px;"></div></div></div></div></div></div>```
    node index.js
```

<div class="WaaZC" id="bkmrk-now%2C-when-you-send-r" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div class="rPeykc" data-hveid="CBwQAQ" data-ved="2ahUKEwiktfqwq9uQAxW6SmwGHZkSPFUQo_EKegQIHBAB" style="margin: 10px 0px;"><span data-huuid="11597112605782550988">Now, when you send requests to your Express server (e.g., by visiting `http://localhost:3000` in your browser), Morgan will log the request details to your console in the specified format (`dev` in this example).</span></div></div></div><div class="WaaZC" id="bkmrk-customizing-morgan%27s" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div class="rPeykc pyPiTc" data-hveid="CB8QAQ" data-ved="2ahUKEwiktfqwq9uQAxW6SmwGHZkSPFUQo_EKegQIHxAB" style="margin: 20px 0px 10px; font-size: 20px; font-weight: 600; line-height: 28px;"><span data-huuid="763672932122439919">Customizing Morgan's Output:</span></div></div></div><div class="WaaZC" id="bkmrk-predefined-formats%3A%C2%A0" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);">- <div class="zMgcWd dSKvsb" data-il="" style="padding-bottom: 0px; padding-top: 0px; border-bottom: none;"><div data-crb-p=""><div class="xFTqob" style="flex: 1 1 0%; min-width: 0px;"><div class="Gur8Ad" style="font-size: 16px; font-weight: 500; line-height: 24px; overflow: hidden; padding-bottom: 4px; transition: transform 200ms cubic-bezier(0.2, 0, 0, 1); display: inline;"><span data-huuid="763672932122442950">**Predefined Formats:** </span></div><div class="vM0jzc" style="color: rgb(10, 10, 10); font-size: 16px; line-height: 24px; display: inline;"><span data-huuid="763672932122442595">Morgan offers various predefined formats like `dev`, `tiny`, `combined`, `common`, and `short`, each providing different levels of detail in the logs.</span></div></div></div></div>
- <div class="zMgcWd dSKvsb" data-il="" style="padding-bottom: 0px; padding-top: 8px; border-bottom: none;"><div data-crb-p=""><div class="xFTqob" style="flex: 1 1 0%; min-width: 0px;"><div class="Gur8Ad" style="font-size: 16px; font-weight: 500; line-height: 24px; overflow: hidden; padding-bottom: 4px; transition: transform 200ms cubic-bezier(0.2, 0, 0, 1); display: inline;"><span data-huuid="763672932122441885">**Custom Format Strings:** </span></div><div class="vM0jzc" style="color: rgb(10, 10, 10); font-size: 16px; line-height: 24px; display: inline;"><span data-huuid="763672932122441530">You can create your own custom format strings using tokens like `:method`, `:url`, `:status`, `:response-time`, `:req[header-name]`, `:res[header-name]`, etc.</span></div></div></div></div>

</div></div><div class="WaaZC" id="bkmrk-javascript-1" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div jsaction="rcuQ6b:npT2md" jscontroller="JegcYe"><div class="ecCNFc" style="border-image: none 100% / 1 / 0 stretch; border-radius: 8px; font-size: 14px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5); margin-bottom: 20px; color: rgb(10, 10, 10); border: 1.2px solid rgb(169, 172, 170);"><div class="zYSUYd" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 8px 8px 0px 0px;"><div class="FS7GEb" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 629.381px;"><div class="dDrxod" style="height: 34px; display: flex; justify-content: space-between; align-items: center; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(229, 237, 255); border-bottom: 1.2px solid rgb(169, 172, 170);"><div class="x7ndcb" style="padding-left: 16px; display: flex; align-items: center; color: rgb(10, 10, 10);">JavaScript</div><div aria-live="polite" class="WDoJJe" style="margin-right: 4px; display: flex; align-items: center;"><button aria-label="Copy code to clipboard" class="hqI3tf B4zsNc" data-ved="2ahUKEwiktfqwq9uQAxW6SmwGHZkSPFUQ9MILegQIKRAB" role="button" style="margin: 0px 2px; background: none; height: 34px; border: none; color: rgb(5, 40, 97); position: relative; top: 2px; outline: 0px;" tabindex="0" title="Copy"><span class="z1asCe wm4nBd" style="display: inline-block; fill: currentcolor; height: 20px; line-height: 20px; position: relative; width: 20px;"><svg aria-hidden="true" enable-background="new 0 0 24 24" focusable="false" height="24" viewbox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><g><rect fill="none" height="24" width="24"></rect></g><g><path d="M16,20H5V6H3v14c0,1.1,0.9,2,2,2h11V20z M20,16V4c0-1.1-0.9-2-2-2H9C7.9,2,7,2.9,7,4v12c0,1.1,0.9,2,2,2h9 C19.1,18,20,17.1,20,16z M18,16H9V4h9V16z"></path></g></svg></span></button></div></div></div></div><div class="QQjpRc" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 0px 0px 8px 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5);"><div class="FS7GEb" jsname="zEXxAd" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 629.381px;"></div></div></div></div></div></div>```
    app.use(morgan(':method :url :status :response-time ms - :res[content-length]'));
```

<div class="WaaZC" id="bkmrk-custom-tokens%3A%C2%A0you-c" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);">- <span data-huuid="453128087640776427">**Custom Tokens:** </span><span data-huuid="453128087640775888">You can define your own custom tokens using `morgan.token()`:</span>

</div></div><div class="WaaZC" id="bkmrk-javascript-2" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: 700ms cubic-bezier(0.2, 0, 0, 1) 200ms 1 normal backwards running response-text-colorshift, 400ms cubic-bezier(0, 0, 0.2, 1) 83ms 1 normal backwards running response-text-fadein; color: rgb(10, 10, 10);"><div jsaction="rcuQ6b:npT2md" jscontroller="JegcYe"><div class="ecCNFc" style="border-image: none 100% / 1 / 0 stretch; border-radius: 8px; font-size: 14px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5); margin-bottom: 20px; color: rgb(10, 10, 10); border: 1.2px solid rgb(169, 172, 170);"><div class="zYSUYd" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 8px 8px 0px 0px;"><div class="FS7GEb" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 629.381px;"><div class="dDrxod" style="height: 34px; display: flex; justify-content: space-between; align-items: center; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(229, 237, 255); border-bottom: 1.2px solid rgb(169, 172, 170);"><div class="x7ndcb" style="padding-left: 16px; display: flex; align-items: center; color: rgb(10, 10, 10);">JavaScript</div><div aria-live="polite" class="WDoJJe" style="margin-right: 4px; display: flex; align-items: center;"><button aria-label="Copy code to clipboard" class="hqI3tf B4zsNc" data-ved="2ahUKEwiktfqwq9uQAxW6SmwGHZkSPFUQ9MILegQIMBAB" role="button" style="margin: 0px 2px; background: none; height: 34px; border: none; color: rgb(5, 40, 97); position: relative; top: 2px; outline: 0px;" tabindex="0" title="Copy"><span class="z1asCe wm4nBd" style="display: inline-block; fill: currentcolor; height: 20px; line-height: 20px; position: relative; width: 20px;"><svg aria-hidden="true" enable-background="new 0 0 24 24" focusable="false" height="24" viewbox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><g><rect fill="none" height="24" width="24"></rect></g><g><path d="M16,20H5V6H3v14c0,1.1,0.9,2,2,2h11V20z M20,16V4c0-1.1-0.9-2-2-2H9C7.9,2,7,2.9,7,4v12c0,1.1,0.9,2,2,2h9 C19.1,18,20,17.1,20,16z M18,16H9V4h9V16z"></path></g></svg></span></button></div></div></div></div><div class="QQjpRc" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 0px 0px 8px 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5);"><div class="FS7GEb" jsname="zEXxAd" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 629.381px;"></div></div></div></div></div></div>```
    morgan.token('host', function(req, res) {
        return req.hostname;
    });
    app.use(morgan(':method :host :status'));
```

<div class="WaaZC" id="bkmrk-logging-to-a-file%3A%C2%A0y" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: 700ms cubic-bezier(0.2, 0, 0, 1) 200ms 1 normal backwards running response-text-colorshift, 400ms cubic-bezier(0, 0, 0.2, 1) 83ms 1 normal backwards running response-text-fadein; color: rgb(10, 10, 10);">- <span data-huuid="4716932418881534909">**Logging to a File:** </span><span data-huuid="4716932418881533724">You can configure Morgan to write logs to a file instead of the console by providing a `stream` option:</span>

</div></div><div class="WaaZC" id="bkmrk-javascript-3" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: 700ms cubic-bezier(0.2, 0, 0, 1) 200ms 1 normal backwards running response-text-colorshift, 400ms cubic-bezier(0, 0, 0.2, 1) 83ms 1 normal backwards running response-text-fadein; color: rgb(10, 10, 10);"><div jsaction="rcuQ6b:npT2md" jscontroller="JegcYe"><div class="ecCNFc" style="border-image: none 100% / 1 / 0 stretch; border-radius: 8px; font-size: 14px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5); margin-bottom: 20px; color: rgb(10, 10, 10); border: 1.2px solid rgb(169, 172, 170);"><div class="zYSUYd" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 8px 8px 0px 0px;"><div class="FS7GEb" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 629.381px;"><div class="dDrxod" style="height: 34px; display: flex; justify-content: space-between; align-items: center; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(229, 237, 255); border-bottom: 1.2px solid rgb(169, 172, 170);"><div class="x7ndcb" style="padding-left: 16px; display: flex; align-items: center; color: rgb(10, 10, 10);">JavaScript</div><div aria-live="polite" class="WDoJJe" style="margin-right: 4px; display: flex; align-items: center;"><button aria-label="Copy code to clipboard" class="hqI3tf B4zsNc" data-ved="2ahUKEwiktfqwq9uQAxW6SmwGHZkSPFUQ9MILegQINxAB" role="button" style="margin: 0px 2px; background: none; height: 34px; border: none; color: rgb(5, 40, 97); position: relative; top: 2px; outline: 0px;" tabindex="0" title="Copy"><span class="z1asCe wm4nBd" style="display: inline-block; fill: currentcolor; height: 20px; line-height: 20px; position: relative; width: 20px;"><svg aria-hidden="true" enable-background="new 0 0 24 24" focusable="false" height="24" viewbox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><g><rect fill="none" height="24" width="24"></rect></g><g><path d="M16,20H5V6H3v14c0,1.1,0.9,2,2,2h11V20z M20,16V4c0-1.1-0.9-2-2-2H9C7.9,2,7,2.9,7,4v12c0,1.1,0.9,2,2,2h9 C19.1,18,20,17.1,20,16z M18,16H9V4h9V16z"></path></g></svg></span></button></div></div></div></div><div class="QQjpRc" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 0px 0px 8px 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5);"><div class="FS7GEb" jsname="zEXxAd" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 629.381px;"></div></div></div></div></div></div>```
    const fs = require('fs');
    const path = require('path');

    const accessLogStream = fs.createWriteStream(path.join(__dirname, 'access.log'), { flags: 'a' });

    app.use(morgan('combined', { stream: accessLogStream }));
```