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
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
### Body-Parser and URL Encoded

`Body-parser` is a parsing middleware package that is built into Express.js so like all packages they must be installed before use.

```
npm install body-parser --save
```

After installation you can use the body-parser object in your code

```js
var bodyParser = require('body-parser')
```

`body-parser` has many different middleware options for different formats but we will be focusing on `urlencoded`. URL encoded is the way your request is formatted. Generally speaking the format of a URL encoded resquest is a string of the names and vaules of your request where the names are attached to the values with an `=` sign and each variable is sperated with `&`. A general form of a URL encoded request would look like this `url/name1=value1&name2=value2`.

When we use `body-parser` we have to specify which format is being taken into the middleware. Since we are using `urlencoded` we would specify this and our general call to `body-parser` would look like this

```js
bodyParser.urlencoded([options])
```

The options are paramaters that can be passed to `body-parser` that do additional tasks. An important option is the `extended` option. `extended` determines where the URL encoded data is parsed. When extended is set to true, the request is parsed in the **qs** library and returns key-value pairs of any type. When extended is set to false, the request is parsed in the **querystring** library and returns key-value pairs of strings or arrays. `Extended` set to a default true unless otherwise specified.

Other options of `bodyParser.urlencoded([options])` are displayed below:

| Option | Description |
| -------------- | ------------------------------------------------------------ |
| inflate | If `true`(default), deflated (compressed) bodies will be inflated. If `false`, deflated bodies are rejected. |
| limit | Controls the maximum request body* size. |
| parameterLimit | Controls the maximum number of parameters that are allowed in the URL-encoded data |
| type | Determines what media type the middleware will parse |

###

*The request body is the code that contains all the data that is being passed to a request. It must be parsed to be useable and that is why body-parser is a very important module.
33 changes: 33 additions & 0 deletions Node_Week_2/expressworks/Concept_Cards/Importing in Express.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
### Installing and Importing Modules

Node.js has 3 different types of modules that can be accessed through your code. Built-in modules do not need to be installed and can be used just by importing them into your code. NPM Modules are thrid party modules that can be installed into a `node_modules` folder through the terminal and accessed with Node.js. Express is an example of a NPM module. Lastly there are local modules that are accessed by providing a file path to your module. These are in the form `./`,`/`,or`../`.

To access these modules in our code we have to import them. We normally do this with `require('module_name')`. The parameter passed into `require()` is the nam e of the module you are trying to import. Here's what it looks like to import Express into our code

```js
var express = require('express')
```

Breaking this line down, `var` is used as standard JS syntax for a variable, `express` is the name we assign the variable that contains the express module, and we use `require()` to import the module `express`.

Some important files that you should be familiar with are `package.json` and `package-lock.json` files. Both serve a similar goal: To keep track of the exact version of every package that is installed so that the project is updateable when packages are updated too. A `package.json` file is part of all nmp packages and stores all the metadata (data about the data) of the project, like version, description, licensing, ect. . It gives information to npm to help with identification and project dependencies.

Even though there is a lot of information in the `package.js` file, Node only accesses 2, name and version, where name is the name of the project and version is the version of the package installed. Version will appear like this:

```json
"version" = "#.#.#"
```

where each hastag is an integer representing major, minor, patch respectively. Using semver notation you can auto-update versions. A `~` before the version number means you only want to update patch releases, and a `^` before the version number means you only want to udpate patch and minor releases.

Other meta properties found in `package.js` include:

| Meta Properties | |
| --------------- | ------------------------------------------------------------ |
| description | a quick description of the project |
| keywords | tags that identify the project |
| main | main entry point for library |
| dependencies | lists modules that the project depends on, installs them when project is installed through nmp |

`package-lock.json` is very similar to `package.json`, the change is that instead of alllowing for updating versions of a package that could potentially introduce bugs into your code, `package-lock.json` "locks" the installed version number for the project such that when other users download and run it there should be no issues with version numbers. You can still update your dependencies by running `npm udpate`.

18 changes: 18 additions & 0 deletions Node_Week_2/expressworks/Concept_Cards/Middleware.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
### Middleware

Middleware is any software that connects operating systems like Linux, Windows, and MacOS to communication protocols and applications that run on it. We have already encountered middleware in the form of APIs. They help tie seperate applications together by "translating" between them and can allow clients to access high level programs without understanding the entire complexity of the program. Middleware can be used to optimize already existing applications as well as help create new ones efficiently.

With Express, middleware functions are used to execute code, change requests and response objects, and call more middleware functions. Middleware functions modify **req** and **res** objects and can streamline projects by preventing repetitive code blocks. Additionally, they can add a layer of security to your program by preventing users from accessing certian atributes of your code. An example of Middleware is the `body-parser` module that is discussed in depth in another concept card.

To use middleware in your program, we first have to import it like a module, we just use the `require` function and pass the name of the middleware as a parameter. To actually use the middleware we simply call the `use` function and pass any necessary parameters into it. If you want to see an example of middleware being executed it can be found with the `body-parser` concept card.

A general format would just look like this:

```js
var name = require("middleware_name")
app.use(name())
```



###
17 changes: 17 additions & 0 deletions Node_Week_2/expressworks/Concept_Cards/Nodemon.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
### Nodemon

Nodemon is an application that starts your server and restarts the server with every change you make in your program. We use it to start the local server accessed by express (`http://localhost:3000 `).

Like all third party applications we must install nodemon through console.

```
npm install nodemon
```

Here we dont need `--save` because nodemon is a global package. To use nodemon it is just like justing node. When we want to run ur program we just type

```
nodemon file.js
```

After you run the program, just head to server on a web browser and see your result!