Welcome to JSCRIPT 400 - Server Side Development with JavaScript
During class, we will be using repositories like these to develop skills and solidify concepts. I would recommend reading through the instructions before coming to class to prime yourself to the material.
Part of your homework assignment for each class will be to make a Pull Request against this repository. Your Pull Request should include answers to any questions in this document.
As we work together on this during class, I would encourage you to make comments next to code we write. Explain what is happening in your own words so that later you have those notes as a reference.
- Use NodeJS APIs to interact with files and 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
To complete this lesson, make sure that node and npm is installed and can be run from the command line. My versions of each are as follows:
$ node -v
v12.2.0
$ npm -v
6.9.0- Fork & Clone this repository
-
Question: What is the difference between forking and cloning a repository as opposed to just cloning a repository?
-
Your Answer: Forking creates a separate copy of the whole repo under a different user, where as cloning is just making a copy, if it is done without forking, it just means that you would like to work on it separately
- Run
npm init -yfrom the command line
-
Question: What does
npm initdo? How does the-yflag modify that command? -
Your Answer: Creates a pacakage.json file and then outputs the same. The -y flag says yes to all the questions.
- 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 = name of your project scripts = scripts that are used as a part of any commands that you would run from your command line It can be npm commands , react commands etc etc licesne = open source licsense or any other custom licesnse that you want to use for your application
- Create a
.gitignorefile
-
Question: What is the purpose of the
.gitignorefile? What is the significance of a "dot-file?" -
Your Answer: Used to store stuff that is not uploaded. Usually used to store API Keys etc, basically private stuff
- Create an
index.jsfile with the following contents:console.log('Hello, Node!')
-
Question: From the command line, how can you run this file?
-
Your Answer: Node index.js will run this file
- Run
npm testfrom the command line
-
Question: What happens and how is this related to what is in the
package.jsonfile? -
Your Answer: No test specified error is thrown and it outputs/echos the details in the pacakge.json file
- 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
- 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: npm run my-file if we are using standard npm commands we dont need run, else we need to specify run
-
Create a new file called
profile.js. Inside the file, copy the following but replace<your-name>with your name:module.exports = '<your-name>'
Add the following to your
index.jsfile. Then, run your file.const profile = require('./profile.js') console.log(profile)
-
Question: What gets logged? Why?
-
Your Answer: Hello World is logged first followed by my name. Exported the code from outside of index.js and pulled in the code into index.js and executed the same
-
Question: What is
module.exportsand what is its type in JavaScript? What isrequireand what is its type in JavaScript? -
Your Answer: require is a function type and module.exports is a special object
- We can only export one thing from files when using Node. With that said, export both your name and your birthday from the
profile.jsfile.
-
Question: What are some ways you can solve this problem?
-
Your Answer: Package everything you need into an object and export/import the same
- Add the following to your
index.jsfile. Then, run your file.const path = require('path') console.log(path.resolve())
-
Question: What is
pathand where does it come from? -
Your Answer: Path comes with node. since there is no ./ it is not something we created but something that comes as a module built in with node
- Install the moment package
-
Question: What command can you run to install this package?
-
Your Answer: npm install moment
- On your own, use this package in the
index.jsfile
-
Question: Do you need to use a
./to require the package? Why or why not? -
Your Answer: Since this is not a file we dont need ./. Since we are using a package we dont need to use ./
- Move your
profile.jsfile into asrc/folder. Update the path in yourindex.jsfile to ensure everything continues to work.