Backend Developer Training - TechCrush
Document Details
Uploaded by MerryIron7261
Tags
Summary
This document covers the basics of backend development, focusing on the Node Package Manager (NPM). It details the purpose of NPM, its history, basic commands, and how to create your own package. It includes explanations and examples of Package.json file, different versioning schemes (SemVer).
Full Transcript
Becoming a Backend Developer WEEK TWO, CLASS ONE Backend Development: NPM and Modules WEEK TWO, CLASS ONE Table of contents 01 02 Node Package Manager Understanding Understand...
Becoming a Backend Developer WEEK TWO, CLASS ONE Backend Development: NPM and Modules WEEK TWO, CLASS ONE Table of contents 01 02 Node Package Manager Understanding Understanding NPM Package.json Diving deeper into packages in NPM 03 Writing your own Package Writing your first package Disclaimer: This training material belongs to TechCrush and shouldn’t be shared 3 01 Node Package Manager Understanding NPM Disclaimer: This training material belongs to TechCrush and shouldn’t be shared 4 Node Package Manager npm (Node Package Manager) is the default package manager for Node.js. It's a command-line tool that allows developers to easily share and reuse code, and manage project dependencies. Why is it important? It simplifies the process of installing, updating, and managing third-party libraries and tools. Brief history 1. Created by Isaac Z. Schlueter in 2010. 2. Became the official package manager for Node.js in 2011. 3. Now maintained by npm, Inc., a subsidiary of GitHub (owned by Microsoft). Disclaimer: This training material belongs to TechCrush and shouldn’t be shared 5 Basic npm commands you must know # Initialize a new Node.js project # Uninstall a package npm init npm uninstall package-name # Install a package and add to # Update packages dependencies npm update npm install package-name # List installed packages # Install a specific version of a npm list package npm install package-name@version # Run a script defined in package.json # Install a package as a dev npm run script-name dependency npm install package-name --save- dev Disclaimer: This training material belongs to TechCrush and shouldn’t be shared 6 02 Understanding Package.json Diving deeper into packages in NPM Disclaimer: This training material belongs to TechCrush and shouldn’t be shared 7 Understanding Package.json The package.json file is the heart of any Node.js project. It contains metadata about the project and lists its dependencies. { 1. name: The name of your project "name": “techcrush-project", 2. version: The current version of your "version": "1.0.0", project "description": "A project to explain npm", "main": "index.js", 3. description: A short description of your "scripts": { project "start": "node index.js", 4. main: The entry point of your application "test": "jest" 5. scripts: Custom scripts that can be run }, with `npm run` "dependencies": { "express": "^4.17.1" 6. dependencies: Packages required for the }, application to run "devDependencies": { 7. devDependencies: Packages only "jest": "^27.0.6" needed for development and testing } } Disclaimer: This training material belongs to TechCrush and shouldn’t be shared 8 Understanding Package.json - SemVer Semantic Versioning is a versioning scheme that uses a three-part version number: MAJOR.MINOR.PATCH MAJOR: Incompatible API changes MINOR: Add functionality (backwards-compatible) PATCH: Bug fixes (backwards-compatible) In `package.json`, you can specify version ranges: `"express": "4.17.1"` - Exact version `"express": "^4.17.1"` - Compatible with 4.17.1 or later, but < 5.0.0 `"express": "~4.17.1"` - Compatible with 4.17.1 or later, but < 4.18.0 `"express": "*"` - Latest version (not recommended for production) Disclaimer: This training material belongs to TechCrush and shouldn’t be shared 9 03 Writing your own Package Writing your first package Disclaimer: This training material belongs to TechCrush and shouldn’t be shared 10 Writing your own Package To create your own npm package 1. Create a new directory and initialize it mkdir my-own-package cd my-own-package npm init Disclaimer: This training material belongs to TechCrush and shouldn’t be shared 11 Writing modules for your package In Node.js, use require and module.exports for modules (remember in the last class, I listed global objects in Node.js. module.exports is used to export a function or class from a file. require is used to import the function into another file. This ensures that you do not have a messed-up, overbloated, complex or heavy file (Make it simple!). // Example: Exporting and Importing // In another file (main.js): const greet = require('./file.js'); // In file.js greet(); module.exports = function greet() { console.log("Hello from file.js!"); }; Disclaimer: This training material belongs to TechCrush and shouldn’t be shared 12 Writing your own Package 3. Publish to npm npm login npm publish Best practices for module development 1. Write clear and concise documentation 2. Include examples in your README.md 3. Use semantic versioning 4. Keep your dependencies up to date 5. Write tests for your module 6. Use continuous integration (CI) tools Disclaimer: This training material belongs to TechCrush and shouldn’t be shared 13 Importing and Using a Package This guide will help you understand how to import different types of modules in Node.js: 1. Local modules (built-in Node.js modules) 2. Third-party modules 3. Custom modules (your own modules) Let us use a project as an example Our Project Format Create a new directory for your project and open it in your Visual Studio Code Initialize a new Node.js project: npm init –y Install a third-party module (we'll use 'chalk' for colored console output): npm install colors Create a file named `greetings.js` in a `my_module` directory for your custom module. For local module, we will use `path`. Disclaimer: This training material belongs to TechCrush and shouldn’t be shared 14 Importing and Using a Package (cont’d) // In the index.js // Example: Exporting and Importing // 1. Importing a local (built-in) module // In greetings.js const path = require('path'); function sayHello(name) { return `Hello, ${name}!`; // 2. Importing a third-party module } const colors = require(‘colors'); // 3. Importing a custom module function sayGoodbye(name) { const greetings = require(‘./my_module/greetings'); return `Goodbye, ${name}!`; } // Using the local (built-in) module console.log(colors.blue('Current file:', module.exports = { path.basename(__filename))); sayHello, // Using the third-party module sayGoodbye console.log(colors.green('This text is green!')); }; // Using the custom module const name = 'John'; console.log(colors.yellow(greetings.sayHello(name))); console.log(colors.red(greetings.sayGoodbye(name))); Disclaimer: This training material belongs to TechCrush and shouldn’t be shared 15 Important Node.js built-in modules (appendix) fs (File System): For file operations (read, write, etc.) path: For working with file and directory paths http/https: For creating HTTP/HTTPS servers and making requests url: For URL resolution and parsing util: For utility functions crypto: For cryptographic functionality os: For operating system-related operations events: For working with events stream: For handling streaming data querystring: For parsing and formatting URL query strings buffer: For handling binary data child_process: For spawning child processes cluster: For splitting a single Node process into multiple processes dns: For DNS lookups and name resolution net: For creating TCP servers and clients readline: For reading input from a readable stream one line at a time string_decoder: For decoding buffer objects into strings timers: For executing a function after a given number of milliseconds tls: For implementing TLS and SSL protocols zlib: For compression and decompression (gzip, deflate, etc.) Disclaimer: This training material belongs to TechCrush and shouldn’t be shared 16 Hands-on assignment Create a Node.js application that converts temperatures between Celsius and Fahrenheit Initialize NPM and update your package.json file accordingly. Create a new file called index.js in your work folder to serve as your main file. Create a file, converter,js, in the same work folder to serve as the module file your conversion functions, and, then import it into your main file. There should be two functions in this file which must collect only one variable i.e. the numbers you want to convert. You do not need any user input. Only specify the variables containing the data you want to convert. Console log your values for the two functions. Clue: The formulas for conversion are as follows (where x is the initial number you want to convert): To Celsius = (x - 32) * 5/9; To Fahrenheit = (x * 9/5) + 32; Make the answer simple. Document your rough sketch (pen, pencil, screenshots, etc.) for full marks. No magic numbers. No non-descriptive variable names Please provide your answers in this form – https://forms.gle/5b9jSHsM3QhqpDKN7 Disclaimer: This training material belongs to TechCrush and shouldn’t be shared 17 Thank You! Disclaimer: This training material belongs to TechCrush and shouldn’t be shared 18