__dirname
: the path of the current file without the file name.__filename
: the path of the current file including the file name.Event: is an signal that broadcast over the app when specific event occur.
Event listener: is a function in a computer program that waits for an event to occur.
Example of event listener
Event emitter: Js class that enables you to create custom events that can be emitted later
Example of event emitter
Stream: objects that let you read data from a source and write data to a destination at the same time.
how stream work?
Stream split the data into small chunks and when the chunk is ready the stream pipe can handle it.
Example of stream: suppose that we have a very large file named input.txt
and we want to copy its content to another file output.txt
if we use the normal way read all the file and store it in the memory, then write it to the output.txt
this may take a long time and the memory resource will be allocated for only this process,
but if we use streams the input file will be split into chunks and when a chunk is ready it will be delivered to the write stream to write it to the output file this will reduce the time and memory usage.
Stream types:
__filename
: returns the absolute path of the current file.__dirname
: returns the absolute path of the directory containing the current file.process.cwd()
: returns the current working directory of the Node.js process ,from where i started this process ?Example:
Output
As you can see process.cwd() returns the current working directory of the Node.js process, which is the directory from which the process was started.
PATH MODULE
path.dirname("/the-path"):
Returns the directory path from a file path same as __dirname
global.path.basename("/the-path"):
Returns the base filename from a file path.path.extname("/the-path"):
Returns the file extension from a file path.path.parse("/the-path"):
Parses a file path into an object with root, dir, base, name, and ext properties.path.join("/the-path"):
Joins one or more path segments together into a single path, its recomeended to use path.join() because different operating systems use different path separators to represent file paths.npm stands for Node Package Manager. It is a command-line tool used to install, manage, and publish packages (libraries and tools) for Node.js.
npm init, a command-line tool that is used to create a new package.json file for a Node.js project.
package.json: is a configuration file that is used to define metadata about a Node.js project, including its dependencies, scripts, and other settings.
package-lock.json: is a lock file that is created by NPM to lock down the specific version of each package that is installed in a project. It is used to ensure that all installations of a project use the same version of each package.
node_modules: is a directory that is created by NPM to store all of the packages that a project depends on. When you install a package using NPM, it will be downloaded and stored in the node_modules directory.
to require npm modules you can use the same syntax as requiring any core module
npm (install || i) <package-name>
: this command installs the specified <package-name> as a dependency for your node.js project. "I" stands for "install" and is shorthand for npm install.
npm (install || i)
: used in to install the necessary dependencies listed in the project's package.json file.
npm (install || i) -g <package-name>
: this command is used to install a Node.js package globally on your system.
npm (install || i) --save-dev <package-name>
: this command installs the specified <package-name> as a development dependency for your node.js project, –save-dev flag tells NPM to add the package to the devDependencies section of your package.json file.
npm (uninstall || un) <package-name>
: this command is used to uninstall a package that was previously installed with npm.
npm list
: this command lists all the packages installed in your project.
npm list --depth=1
: this command lists all the packages installed in your project and their level-one dependencies
Example
npm view <package-name>
: this command displays information about the specified <package-name>, including its name, version, description, keywords, and maintainers.
update existing packages:
npm i -g npm-check-updates
: This command installs npm-check-updates globally, this package is a utility tool that helps you keep your Node.js project dependencies up to datencu
: used to check for available updates to your Node.js project dependencies listed in the package.json file.ncu -u
: this command is used to check for available updates to your Node.js project dependencies and replace all available versions, including those that may have breaking changes.npm i
command to install the new version.10.npm search _____
: to search for a specific package or a keyword on the npm registry
Major: This indicates significant changes to the software that may affect its compatibility with other software or systems.
Minor: This indicates smaller updates or improvements to the software that may not affect compatibility.
Batch (or Patch): This indicates a small update or fix to the software, often to address a specific issue or bug that has been discovered since the last release. It usually does not include any new features or major changes.
So, for example, a version number of "3.1.2" would indicate a major version of 3, a minor version of 1, and a batch (or patch) version of 2.
is a metadata file in JSON format, which is used to manage and describe a Node.js project's dependencies, scripts, and other project-specific details.
name: A string that specifies the name of the package.
version: A string that specifies the version of the package.
description: A brief description of the package.
keywords: An array of strings that describes the package in more detail, used for search optimization and categorization.
license: A string that specifies the license under which the package is released.
author: A string or an object that describes the author(s) of the package.
repository: An object that describes the location of the package's source code repository.
dependencies: An object that lists the package's dependencies and their respective versions.
devDependencies: An object that lists the package's development dependencies and their respective versions.
scripts: An object that lists custom scripts that can be executed with npm run.
Example
Application is a software program that is designed to perform a specific task or set of tasks,a project, on the other hand, is a set of related tasks that are undertaken to achieve a specific goal.
Feature | Application | Project |
---|---|---|
Purpose | Designed to perform a specific task or set of tasks | to achieve a specific goal |
Scope | Typically focused on a single task or set of tasks | May involve multiple tasks that are interrelated |
Example: application .jar
file project java code
to build a backend application you need 3 important things.
Is the process of associating HTTP
requests with specific functions(endpoints). This allows you to control how your application responds to different types of requests.
To define a route in express, you use the app.METHOD()
method, where METHOD is the HTTP method, such as GET
or POST
Example:
In the Express.js framework, req, res, and next are three parameters that are passed to every route handler function.
req
: is an object that contains information about the request, such as the URL, the HTTP method, and the headers.
res
: is an object that can be used to send a response back to the client.
next
: is a function that can be used to call the next middleware function in the stack.
1. Query string: is a part of the URL that is used to pass data to the server, the query string is separated from the URL by a question mark ?
and is made up of key-value
pairs.
you can use the req.query
object to access the query string
2. Path parameters: part of the URL that are used to identify a specific resource on the server.
Example: /users/:id
=> /users/1
| /users/2
| /users/3
…etc.
you can use the req.params object to access path parameters.
NOTES:
in the previous example if we request /user/all
the output is “all” because it appeared first but if we flip the order the result well be “by id”.
3. Headers: a way to pass additional information with an HTTP request. Headers are key-value pairs, where the key is the name of the header and the value is the value of the header.
you can use the req.headers object or req.get("HEADER_NAME") function to access headers
NOTE: if you want to use .get
function to access headers, header name must start with a capital letter.
4. body: COMING SOON
is a function that is used to process an HTTP request and response before the end point,used to perform tasks such as:
To use middleware in Express, you can use the app.use()
method
NOTE: middleware next function let you go to the next middleware and if this is the last one in the chain next function move you to the target endpoint.
NOTE: middlewares ordder is important.
NOTE: middlewares layer is shared between all endpoints