owned this note
owned this note
Published
Linked with GitHub
# Engineering
---

---
## Modules
Modular code is code which is separated into independent modules. The idea is that internal details of individual modules should be hidden behind a public interface, making each module easier to understand, test and refactor independently of others.
---
### Core Modules
Node.js has several modules compiled into the binary.
1. http
2. url
3. querystring
4. path
5. fs (file system)
6. util
---
The core modules are defined within Node.js's source and are located in the lib/ folder. Core modules are always preferentially loaded if their identifier is passed to require(). For instance, require('http') will always return the built in HTTP module, even if there is a file by that name.
```javascript=
const module = require('module_name');
```
---
### Local modules
Local modules are modules created locally in your Node.js application. These modules include different functionalities of your application in separate files and folders. You can also package it and distribute it via NPM, so that Node.js community can use it. For example, if you need to connect to MongoDB and fetch data then you can create a module for it, which can be reused in your application.
---
In the Node.js module system, each file is treated as a separate module. For example, consider a file named foo.js:
```javascript=
const circle = require('./circle.js');
console.log(`The area of a circle of radius 4 is ${circle.area(4)}`);
```
On the first line, foo.js loads the module circle.js that is in the same directory as foo.js.
---
Here are the contents of circle.js:
```javascript=
const { PI } = Math;
exports.area = (r) => PI * r ** 2;
exports.circumference = (r) => 2 * PI * r;
```
---
The module circle.js has exported the functions area() and circumference(). Functions and objects are added to the root of a module by specifying additional properties on the special exports object.
Variables local to the module will be private, because the module is wrapped in a function by Node.js. In this example, the variable PI is private to circle.js.
The module.exports property can be assigned a new value (such as a function or object).
---
### The module.exports or exports
is a special object which is included in every JS file in the Node.js application by default. module is a variable that represents current module and exports is an object that will be exposed as a module. So, whatever you assign to module.exports or exports, will be exposed as a module.
---
### Modules in the browser

---
```html=
<script type="module" src="./main.js"></script>
<script type="module">
import {log} from './main.js';
log('Inline module!');
</script>
```
---
## Asynchonrous functions

---
### Why should you use asyncronous forms of functions wherever possible in Node?

---
Because I/O is sloooow

---
### What are error-first callbacks, and why is it important to follow that pattern in your own code?
It's a pattern!
```javascript
fs.readFile('/foo.txt', function(err, data) {
// If an error occurred, handle it (throw, propagate, etc)
if(err) {
console.log('Unknown Error');
return;
}
// Otherwise, log the file contents
console.log(data);
});
```
---
### Why should you avoid using throw in callbacks?
Asynchronous exception is uncatchable because the intended catch block is not present when the asynchronous code is executed. Instead, the exception will propagate all the way and terminate the program.
---
### When might you use the syncronous form of a function instead?

---
## Input / Output
### The FS and Path Modules
---
The Node.js file system module allows you to work with the file system on your computer.
It provides an API for interacting with the file system in a manner closely modeled around standard POSIX functions.
---
To use this module use:
`const fs = require("fs")`
---
Common use for the File System module:
- Read files
- Create files
- Update files
- Delete files
- Rename files
---
### Read Files
Read files such as HTML and host them online
The fs.readFile() method is used to read files on your computer.
---
### Create Files
The File System module has methods for creating new files:
fs.appendFile()
fs.open()
fs.writeFile()
---
### Update Files
The File System module has methods for updating files:
fs.appendFile()
fs.writeFile()
---
### Delete Files
To delete a file with the File System module, use the fs.unlink() method.
---
### Rename Files
To rename a file with the File System module, use the fs.rename() method.
---
## What are some of the issues of working with paths when accessing a file system?
---

- Structuring files into logical paths & keeping them in distinct, well-defined folders
---
- names, naming files
- Not good for modularising, if you change the folder names... uh, oh!
---
- There are functions available in paths module which you'd be hardcoding, i.e. time-wasting!

- No need to install paths, it is part of core modules
---

---
The Path module provides a way of working with directories and file paths.
---
To use the module:
`var path = require('path');`
---

---
## Some Cool 1'z

---
The path.basename() methods returns the last portion of a path.
```javascript=
path.basename('/foo/bar/baz/asdf/quux.html');
// Returns: 'quux.html'
```
```javascript=
path.basename('/foo/bar/baz/asdf/quux.html', '.html');`
`// Returns: 'quux'
```
---
The path.dirname() method returns the directory name of a path.
```javascript=
path.dirname('/foo/bar/baz/asdf/quux');
// Returns: '/foo/bar/baz/asdf'
```
---
The path.extname() method returns the extension of the path, from the last occurrence of the . (period) character to end of string in the last portion of the path.
```javascript=
path.extname('index.html')
//Returns: '.html'
```
---
## Working with URLS
---
### What is a urlObject and how is it structured?
- A URL like
```
http://www.example.com/foo?bar=1#main
```
consists of several different parts -
e.g., the host part
```
(www.example.com)
```
or the search
```
(?bar=1, often called query string)
```
---
- When writing Node.js web server software, you regularly need to access or even manipulate those different parts.
- splitting this string into its different logical parts manually with substring operations or regular expressions is very cumbersome.
- A dedicated library makes this very easy.
---
- the url module provides a parse() method which converts a url string into a urlObject
- The parsed urlObject has various properties denoting different parts of the url
```javascript=
var url=require('url');
var address = "http://gremlin:sparks@host.domain.com:4444/somewhere?q=one";
var my_url=url.parse(address);
```
my_url will be the following object
```javascript=
{
protocol: "http:"
host: "gremlin:sparks@host.domain.com:4444"
hostname:"host.domain.com"
port: "4444"
auth: "gremlin:sparks"
pathname: "/somewhere"
search:"?q=one"
query:"q=one"
}
```
---
Note the query property: anything that comes after the first ? in the url is the 'query string'
```
http://example.com/path/to/page?name=ferret&color=purple
the query string here is "name=ferret&color=purple"
```
this can have any number of different properties and values depending on the webpage
node provides another module called querystring to separate these properties into an object
---
In conclusion, the url module, especiall url.parse is useful to build functionality that depends on different parts of a url.
for example to separate an endpoint from the url, or to separate the query string from the url.
basically we have convenient access to the different parts of a url as object properties and values so we can use them within our code.
---
### Why is it important to be able to turn JavaScript objects into querystrings and back again?
---
one of the original use cases for query strings was web forms.
when we submit a form, the text entered in different fields is encoded into the url as
```
field1=value1&field2=value2&field3=value3
```
the querystring module allows us to convert this to a JSON object using querystring.parse() and back into a string, using querystring.stringify()
---
```
field1=value1&field2=value2&field3=value3
```
querystring.parse() would turn the above string into an object like this:
```javascript=
{
field1: value1,
field2: value2,
field3: value3
}
```
---
- having these properties as object values makes it much easier to use them in logical expressions like if statements,
- or to pass these into functions that process them
- (taking the CMS example from the nodegirls workshop, to take the form info and populate a blogpost on the page with these values)
---
### Why is it a bad idea to build a query string manually from other strings (think about URL encoding and escape characters)?
---
Take a look at this hypothetical input into a textbox
```
chocolate is cool 8628^#^#^#*9@(*&
```
the query string version of that string would look like this
```
chocolate+is+cool+8628%5E%23%5E%23%5E%23%2A9%40%28%2A%26+
```
this is a process known as url encoding. special characters are 'escaped' so the browser can read them properly.
---
- basically A URL is composed from a limited set of characters belonging to the US-ASCII character set. These characters include digits (0-9), letters(A-Z, a-z), and a few special characters ("-", ".", "_", "~").
- there are also some 'reserved' characters that have special meaning within URLs.
- examples include ```?, /, #, : ```
- any parts of the url containing data(pathname, querystring etc)must not contain these characters.
---
- So keeping these limitations in mind, other special characters are 'escaped'(converted) to their hex counterparts(these start with a %), and spaces are converted to a '+' in query strings.
- if we were to convert something(say form data) manually to a query string, we'd have to painstakingly create functionality to replace all these special characters with their hex keys, and spaces with '+'
- using the built in stringify() method is much easier in this case.
---
## THANK YOU!!
---
## References
[NodeJS documentation - modules](https://nodejs.org/api/modules.html)
[NodeJS modules](https://www.tutorialsteacher.com/nodejs/nodejs-modules)
[ES6 modules in the browser](https://medium.com/ghostcoder/using-es6-modules-in-the-browser-5dce9ca9e911)
[W3 Schools File System](https://www.w3schools.com/nodejs/nodejs_filesystem.asp)