From 809b4aa92f7ef8728cc1e0f3de1fa3b288c90d54 Mon Sep 17 00:00:00 2001 From: rubini124 Date: Mon, 1 Jul 2019 00:21:39 -0700 Subject: [PATCH] Class notes --- readme.md | 71 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 43 insertions(+), 28 deletions(-) diff --git a/readme.md b/readme.md index ccca3f4..577b7f8 100644 --- a/readme.md +++ b/readme.md @@ -10,15 +10,15 @@ As we work together on this during class, I would encourage you to make comments ## Core Learning Objective -* Use NodeJS APIs to interact with files and the web +- Use NodeJS APIs to interact with files and the web ## Sub-Objectives -* Setup a basic NPM project -* Run your project from the command line with scripts -* Export and require your own files -* Require and use core Node libraries -* Install, require, and use packages from the web +- Setup a basic NPM project +- Run your project from the command line with scripts +- Export and require your own files +- Require and use core Node libraries +- Install, require, and use packages from the web ### Prereqs @@ -40,7 +40,7 @@ $ npm -v * **Your Answer:** ---- +---Forking and cloning creates a copy of the repository on which changes could be made and a pull request created to merge with original repo. In cloning, a local copy is created in the machine where changes could be added and commited and then pushed to remote repo. - [ ] Run `npm init -y` from the command line @@ -48,7 +48,7 @@ $ npm -v * **Your Answer:** ---- +---npm init command initializes the project and creates repository, bugs and homepage in the package.json file. -y flag takes the default project name and settings and does not ask it. - [ ] Take a look at the file that was generated by the previous command @@ -56,7 +56,9 @@ $ npm -v * **Your Answer:** ---- +---name - contains package name +scripts - dictionary containing script commands that are run at various times in the lifecycle of your package. The key is the lifecycle event, and the value is the command to run at that point. By default, creates an empty test script +license - license of the package so that users know how to use the package - [ ] Create a `.gitignore` file @@ -64,7 +66,7 @@ $ npm -v * **Your Answer:** ---- +--- gitignore file specifies intentionally untracked files that Git should ignore. dot-file are files treated as hidden files. - [ ] Create an `index.js` file with the following contents: `console.log('Hello, Node!')` @@ -72,15 +74,22 @@ $ npm -v * **Your Answer:** ---- +---node index.js - [ ] Run `npm test` from the command line -* **Question:** What happens and how is this related to what is in the `package.json` file? +* **Question:** What happens and how is this related to what is in the `package.json` file? * **Your Answer:** ---- +---> w1-node-ecosystem@1.0.0 test C:\Rubini\Technical\Personal\UW\JScript400A\w1-node-ecosystem + +> echo "Error: no test specified" && exit 1 + +"Error: no test specified" +npm ERR! Test failed. See above for more details. + +test Scripts from package.json are run - [ ] Create a new "script" command called "start" that has the following value: `node index.js` @@ -88,7 +97,7 @@ $ npm -v * **Your Answer:** ---- +---npm start - [ ] Change the name of your "start" script to "my-file" @@ -96,28 +105,34 @@ $ npm -v * **Your Answer:** ---- +---npm run my-file - [ ] Create a new file called `profile.js`. Inside the file, copy the following but replace `` with your name: + ```js - module.exports = '' + module.exports = ""; ``` Add the following to your `index.js` file. Then, run your file. + ```js - const profile = require('./profile.js') - console.log(profile) + const profile = require("./profile.js"); + console.log(profile); ``` * **Question:** What gets logged? Why? * **Your Answer:** + Hello, Node! + Rubini + The file profile.js is logged in the console. -* **Question:** What is `module.exports` and what is its _type_ in JavaScript? What is `require` and what is its _type_ in JavaScript? +- **Question:** What is `module.exports` and what is its _type_ in JavaScript? What is `require` and what is its _type_ in JavaScript? -* **Your Answer:** +- **Your Answer:** ---- +---'module.exports' is the object that's actually returned as the result of a require call - it exposes internally scoped things out of module. Here the type is string. +'require' is used to load to the module which to be exposed. Here, the type is string. - [ ] We can only export one thing from files when using Node. With that said, export both your name and your birthday from the `profile.js` file. @@ -125,19 +140,19 @@ $ npm -v * **Your Answer:** ---- +---Wrap them in a collection like object or array and access them. Thus export only one object. - [ ] Add the following to your `index.js` file. Then, run your file. ```js - const path = require('path') - console.log(path.resolve()) + const path = require("path"); + console.log(path.resolve()); ``` * **Question:** What is `path` and where does it come from? * **Your Answer:** ---- +---node gets the path(the folder we are currently in) from the built in module Path. The path.resolve() method resolves a sequence of paths or path segments into an absolute path. - [ ] Install the [moment](https://www.npmjs.com/package/moment) package @@ -145,7 +160,7 @@ $ npm -v * **Your Answer:** ---- +---npm install moment - [ ] On your own, use this package in the `index.js` file @@ -153,11 +168,11 @@ $ npm -v * **Your Answer:** ---- +---No. It depends on the where the file or folder is placed inside the package. - [ ] Move your `profile.js` file into a `src/` folder. Update the path in your `index.js` file to ensure everything continues to work. #### Resources - [Node.js Built-In Modules](https://nodejs.org/dist/latest-v12.x/docs/api/) -- [NPM: Moment](https://www.npmjs.com/package/moment) \ No newline at end of file +- [NPM: Moment](https://www.npmjs.com/package/moment)