npm Flashcards

1
Q

What is npm?

A

npm(Node Package Manager) is the world’s largest software registry. Open source developers from every continent use npm to share and borrow packages, and many organizations use npm to manage private development as well.

npm consists of three distinct components:

  1. The Website: Use the website to discover packages, set up profiles, and manage other aspects of your npm experience. For example, you can set up organizations to manage access to public or private packages.
  2. The Command Line Interface (CLI): The CLI runs from a terminal, and is how most developers interact with npm.
  3. The Registry: The registry is a large public database of JavaScript software and the meta-information surrounding it.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a package?

A

A directory with one or more files of reusable code in it, and also has a file called package.json with some meta-data about this package.

If it has package.json, it is a package.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How can you create a package.json with npm?

A

~/repos/c0621-code-solutions/npm-intro (npm-intro)

λ npm init –yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a dependency and how do you add one to a package?

A

A dependency is another package that your package needs in order to work.

You can add one to a package by:
Ex:
npm install jquery

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What happens when you add a dependency to a package with npm?

A

It downloads the package so it can be used.
The property dependencies get added to package.json:

{
  "name": "npm-intro",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "jquery": "^3.6.0"
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you add express to your package dependencies?

A

npm install express

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What Express application method starts the server and binds it to a network PORT?

A

listen( )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you mount a middleware with an Express application?

A

Using the use/get method.

Examples:

app.use(function (req, res, next) {
res.send(‘Hello World’)
} )

app.use(function (req, res, next) {
console.log(‘Time: %d’, Date.now( ))
next( )
} )

It happens when I request is received in the server.
Event listener for requests.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which objects does an Express application pass to your middleware to manage the request/response lifecycle of the server?

A

req and res.

request and response.

req is a data-model for the request message:
GET / HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: 192.168.1.40:1337
User-Agent: HTTPie/1.0.3
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the appropriate Content-Type header for HTTP messages that contain JSON in their bodies?

A

application/json

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does the express.json( ) middleware do and when would you need it?

A

express.json( ) is a method inbuilt in express to recognize the incoming Request Object as a JSON Object. This method is called as a middleware in your application using the code: app.use(express.json( ));

If the request includes JSON, use it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the significance of an HTTP request’s method?

A

It defines what is to be done with the data.

The method name is just for semantics.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is Webpack?

A

It’s a tool that lets you bundle your JavaScript applications (supporting both ESM and CommonJS), and it can be extended to support many different assets such as images, fonts and stylesheets.

Bundles modules.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you add a devDependency to a package?

A

npm install –save-dev webpack

or

npm i -D webpack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is an NPM script?

A

An npm script is a convenient way to bundle common shell commands for your project.

Command line shortcuts.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you execute Webpack with npm run?

A

Add a script in package.json

Ex:

“scripts”: {
“build”: “webpack”,
“test”: “echo "Error: no test specified" && exit 1”
},

17
Q

What kind of modules can Webpack support?

A

ECMAScript modules. CommonJS modules. AMD modules.

18
Q

What does express.static( ) return?

A

It returns a middleware function. Programmed to look in directory and sort.

19
Q

What is the local __dirname variable in a Node.js module?

A

The directory name of the current module. This is the same as the path.dirname( ) of the __filename.

__dirname is an absolute path.

20
Q

What does the join( ) method of Node’s path module do?

A

The path.join( ) method joins all given path segments together using the platform-specific separator as a delimiter( / ), then normalizes the resulting path.