Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
15 changes: 15 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
console.log('Hello, Node!')
const profile = require('./node_modules/moment/src/profile')
console.log(profile)

const path = require('path')
console.log(path.resolve())

const moment = require('./node_modules/moment/moment.js')
/*this also works
const moment = require('moment')
node knows where to look
*/

const timenow = moment().format('LTS')
console.log(timenow)
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "w1-node-ecosystem",
"version": "1.0.0",
"description": "Welcome to JSCRIPT 400 - Server Side Development with JavaScript",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"my-file": "node index.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/toan-myGitHub/w1-node-ecosystem.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/toan-myGitHub/w1-node-ecosystem/issues"
},
"homepage": "https://github.com/toan-myGitHub/w1-node-ecosystem#readme",
"dependencies": {
"moment": "^2.24.0"
},
"devDependencies": {}
}
89 changes: 75 additions & 14 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ To complete this lesson, make sure that `node` and `npm` is installed and can be
$ node -v
v12.2.0

$ npm -v

6.9.0
```

Expand All @@ -39,64 +39,90 @@ $ npm -v
* **Question:** What is the difference between forking and cloning a repository as opposed to just cloning a repository?

* **Your Answer:**
just cloning a repository - make no linking back to your github repo, no updaing when things are changed
forking and cloning - linking to your github repo, updates can be pushed to online repo


---

- [ ] Run `npm init -y` from the command line

* **Question:** What does `npm init` do? How does the `-y` flag modify that command?

* **Your Answer:**

---
npm init
to initialize a project
promt for input from user
project's name,
project's version,
project's description,

npm init -y
-y stands for yes
generate an empty npm project without going through an interactive process
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not necessarily that it's empty, it's that it sets all the default values.


- [ ] Take a look at the file that was generated by the previous command

* **Question:** What is the purpose of the following keys? "name", "scripts", "license"

* **Your Answer:**

---
name: project's name
scripts: to automate repetitive tasks, put them in scripts if you dont want to type long line into command promt
license: project's license set to ISC Internet Systems Consortium license

- [ ] Create a `.gitignore` file

* **Question:** What is the purpose of the `.gitignore` file? What is the significance of a "dot-file?"

* **Your Answer:**

---
.gitignore
contain files that to be ignore: pets.json
not get push/update to GitHub
files that change all the time while testing/building

dot-file
configuration files
hidden files

- [ ] Create an `index.js` file with the following contents: `console.log('Hello, Node!')`

* **Question:** From the command line, how can you run this file?

* **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?

* **Your Answer:**

---
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This answer doesn't describe what is happening. When you run npm test, the value of test gets run. So, in this case, we get a message printed to the screen and then our program exits.


- [ ] Create a new "script" command called "start" that has the following value: `node index.js`

* **Question:** What will you enter on the command line to run that script?

* **Your Answer:**

---
npm start
> w1-node-ecosystem@1.0.0 start C:\source\Summer2019\w1-node-ecosystem
> node index.js
Hello, Node!

- [ ] Change the name of your "start" script to "my-file"

* **Question:** The same pattern will not work to try and run this script. How can you successfully get this script to run?

* **Your Answer:**

---
my-file is not one of npm command
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct! But then, how would you run it? In this case, you would type: npm run my-file



- [ ] Create a new file called `profile.js`. Inside the file, copy the following but replace `<your-name>` with your name:
```js
Expand All @@ -112,20 +138,38 @@ $ npm -v
* **Question:** What gets logged? Why?

* **Your Answer:**
PS C:\source\Summer2019\w1-node-ecosystem> node index.js
Hello, Node!
Toan Bui


* **Question:** What is `module.exports` and what is its _type_ in JavaScript? What is `require` and what is its _type_ in JavaScript?

* **Your Answer:**

---
module.exports
exported as a module for future use in other js files

ex: in hello.js we have
module.exports = 'Hello world';

require
to use modules from other js files
ex: in index.js we have
const gretting = require('./hello.js');
console.log(greeting);
the output is Hello world

type in JavaScript
String, Number, Boolean, Undefined, Null, Object, Array, Function
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The question above is asking what the types are of module.exports and require specifically. module.exports is an Object whereas require() is a Function.


- [ ] 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.

* **Question:** What are some ways you can solve this problem?

* **Your Answer:**

---
module.exports = {age, name, phone}

- [ ] Add the following to your `index.js` file. Then, run your file.
```js
Expand All @@ -137,26 +181,43 @@ $ npm -v

* **Your Answer:**

---
Hello, Node!
Toan Bui
C:\source\Summer2019\w1-node-ecosystem
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't describe what path is. path is a module built-in to Node that we can use to access information about the file system.



- [ ] Install the [moment](https://www.npmjs.com/package/moment) package

* **Question:** What command can you run to install this package?

* **Your Answer:**

---
npm install moment --save
https://momentjs.com/


- [ ] On your own, use this package in the `index.js` file

* **Question:** Do you need to use a `./` to require the package? Why or why not?

* **Your Answer:**

---
const moment = require('./node_modules/moment/moment.js')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you can do this, you do not need to. You only need to do the following:

const moment = require('moment')

This will find the package in the node_modules/ folder by default.

const timenow = moment().format('LTS')
console.log(timenow)

Hello, Node!
Toan Bui
C:\source\Summer2019\w1-node-ecosystem
7:32:32 PM



- [ ] Move your `profile.js` file into a `src/` folder. Update the path in your `index.js` file to ensure everything continues to work.

const profile = require('./node_modules/moment/src/profile')
console.log(profile)

#### Resources

- [Node.js Built-In Modules](https://nodejs.org/dist/latest-v12.x/docs/api/)
Expand Down