Your Webpage is a front end input / output to your database. We will start building the database and push out from there to make a Web Server and HTML pages with forms that the user can do input and output from the database.
We need Modules because: You project will have files for (1) front end web server, (2) back sqlite, (3) Middle Layer TypeScript Business Objects to implement the business rules and algorithms of your Application.
You need the Node Modules system to do the data plumbing between these 3 partitions.
In this workbook, we will explore the basics of node JS modules and how to import and export them.
Through a series of lab exercises, you will learn how to create and import your own node JS modules, as well as how to use existing modules from the npm registry.
Lab Exercise 1: Importing and Exporting Basic Modules
In this exercise, we will create a simple module and learn how to import and export it.
1. Create a new directory and navigate into it
2. Initialize a new npm project with `npm init -y`
3. Create a new file named `myModule.js`
4. In `myModule.js`, create a new class named `MyModule`
5. Export `MyModule` using `module.exports = MyModule`
6. Create a new file named `index.js`
7. In `index.js`, import `MyModule` using `const MyModule = require('./myModule')`
8. Create a new instance of `MyModule` using `const myModule = new MyModule()`
9. Log the output of a method on `myModule` using `console.log(myModule.someMethod())`
10. Run `node index.js` and verify that the output is correct
Lab Exercise 2: Using npm Modules
In this exercise, we will learn how to import and use npm modules.
1. Use `npm install` to install a package named `lodash`
2. Create a new file named `index.js`
3. In `index.js`, import `lodash` using `const _ = require('lodash')`
4. Use `_.join` to join two arrays together
5. Use `_.upperCase` to uppercase a string
6. Use `_.parseInt` to parse an integer from a string
7. Log the output of these methods using `console.log()`
8. Run `node index.js` and verify that the output is correct
Lab Exercise 3: Creating and Exporting Classes
In this exercise, we will create and export a class, and then import and use it in another file.
1. Create a new file named `myClass.js`
2. In `myClass.js`, create a new class named `MyClass`
3. Export `MyClass` using `module.exports = MyClass`
4. In a new file named `index.js`, import `MyClass` using `const MyClass = require('./myClass')`
5. Create a new instance of `MyClass` using `const myClass = new MyClass()`
6. Log the output of a method on `myClass` using `console.log(myClass.someMethod())`
7. Run `node index.js` and verify that the output is correct
That's it! This workbook should give you a solid understanding of how to import and export basic modules, as well as how to use existing npm modules.
Start Here:
Node.js Module Import and Export System: Student Lab Workbook
Introduction:
In this lab, you will explore the Node.js module system, which is a core feature of Node.js. Modules allow you to organize your code into reusable pieces, which can be imported and used in other parts of your application. By the end of this lab, you will have a solid understanding of how to create, export, and import modules in Node.js.
Lab Exercise 1: Creating and Exporting a Simple Module
Objective: Learn how to create a module and export its functionalities.
Task: Create a new file named mathOperations.js.
In this file, define two functions: add and subtract.
javascriptCopy code
// mathOperations.js
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
Now, let's export these functions so they can be used in other files.
javascriptCopy code
module.exports = {
add,
subtract
};
Explanation: Here, we're using the shorthand property names introduced in ES6. This is equivalent to {add: add, subtract: subtract}.
Lab Exercise 2: Importing and Using a Module
Objective: Learn how to import and utilize functions from an exported module.
Task: Create a new file named calculator.js.
In this file, import the mathOperations module you created in the previous exercise.
javascriptCopy code
const mathOps = require('./mathOperations');
Use the imported add and subtract functions to perform some calculations.
javascriptCopy code
const sum = mathOps.add(5, 3);
const difference = mathOps.subtract(5, 3);
console.log(`Sum: ${sum}`);
console.log(`Difference: ${difference}`);
Run the calculator.js file using Node.js:
bashCopy code
node calculator.js
Expected Output:
makefileCopy code
Sum: 8
Difference: 2
Explanation: By using the require function, we can import the functionalities of another module into our current file. This allows for modular and organized code.
Lab Exercise 3: Advanced Module Patterns
Objective: Explore some advanced module patterns in Node.js.
Task: Modify the mathOperations.js file to use a class-based approach.
javascriptCopy code
// mathOperations.js
class MathOperations {
static add(a, b) {
return a + b;
}
static subtract(a, b) {
return a - b;
}
}
module.exports = MathOperations;
Update the calculator.js file to use the new class-based module.
javascriptCopy code
const MathOps = require('./mathOperations');
const sum = MathOps.add(7, 4);
const difference = MathOps.subtract(7, 4);
console.log(`Sum: ${sum}`);
console.log(`Difference: ${difference}`);
Run the calculator.js file again:
bashCopy code
node calculator.js
Expected Output:
makefileCopy code
Sum: 11
Difference: 3
Explanation: This exercise introduced the concept of exporting a class from a module. Classes can encapsulate related functionalities and offer a structured way to organize code.
Conclusion:
Congratulations on completing the lab exercises! You've now gained hands-on experience with the Node.js module system.
This knowledge will be invaluable as you continue to develop more complex Node.js applications. Remember, modular code is cleaner, more maintainable, and easier to debug. Happy coding!
Want to print your doc? This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (