Professional Documents
Culture Documents
Glotech
Content
- Introduction
- Asynchronous programming
- Http server
- REST
- Express Js
- Database connection
Introduction
- Node.js is an open source server environment
- Node.js runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
- Node.js uses Google Javascript V8 Engine to execute code
- Everything inside Node.js runs in single thread
- Node.Js can be downloaded and installed at https://nodejs.dev/download/
C++
Node.js Hello World application
Node Package Manager
The NPM program is installed on your computer when you install Node.js
Install package:
1. Open terminal
2. Run the command `npm init` in the terminal. At this point you should be prompted
for some information. When you are done you should have a file called
`package.json` in your project
3. Running your first problem with following command `node <entry_file>`
Creating a project
The package.json configuration file
- The package.json file is kind of a manifest for your project. It can do a lot of things,
completely unrelated. It's a central repository of configuration for tools, for example.
It's also where npm store the names and versions for all the installed packages.
}
npm global or local packages
Local packages: are installed in the directory where you run npm install <package-
name>, and they are put in the node_modules folder under this directory
Global packages: are all put in a single place in your system (exactly where depends on
your setup), regardless of where you run npm install -g <package-name>
A package should be installed globally when it provides an executable command that you
run from the shell (CLI), and it's reused across projects.
npm dependencies and devDependencies
When you go in production, if you type npm install and the folder contains a package.json
file, they are installed, as npm assumes this is a development deploy.
The package.json configuration file
- name: application’s name
- version: application’s version
- description: brief description of the application
- main: application’s entry point
- scripts: list of scripts you can run
- dependencies: list of npm packages installed as dependencies
- devDependencies: list of npm packages installed as development dependencies
https://nodejs.dev/learn/the-package-json-guide
Exercise
Event loop executes tasks in process.nextTick queue first, and then executes promises
microtask queue, and then executes macrotask queue.
Event loop execution order
Node.JS event loop strategy
- Using setTimeOut, setImmediate, process.nextTick to run less importance task in
the current business code
Node.JS event loop strategy
Write an nodejs application to read all country name of the given JSON API
Exercise
Write Node.Js application to read and process a big file.
- Write a program that will print out the total number of lines in the file.
- Notice that the 8th column contains a person’s name. Write a program that loads in this
data and creates an array with all name strings. Print out the 432nd and 43243rd names.
- Notice that the 5th column contains a form of date. Count how many donations occurred
in each month and print out the results
- Notice that the 8th column contains a person’s name. Create an array with each first
name. Identify the most common first name in the data and how many times it occurs.
Data: https://www.fec.gov/files/bulk-downloads/2018/indiv18.zip
Exercise
Investigate woker_threads
Http server
Http protocol
- HTTP is a protocol for fetching resources such as HTML documents
- HTTP is an extensible protocol which has evolved over time. It is an application
layer protocol that is sent over TCP, or over a TLS-encrypted TCP connection,
though any reliable transport protocol could theoretically be used
Http protocol
Http protocol
Http Server with Node.Js
Http Server with Node.Js
Exercise
- Create your first Http server to serve request from users
REST
- REST stands for REpresentational State Transfer.
- REST is web standards based architecture and uses HTTP Protocol.
REST resources
For example, we are going to create a pretty common (and very practical) REST API for a
resource called users.
- POST on the endpoint /users (create a new user)
- GET on the endpoint /users (list all users)
- GET on the endpoint /users/:userId (get a specific user)
- PATCH on the endpoint /users/:userId (update the data for a specific user)
- DELETE on the endpoint /users/:userId (remove a specific user)
Express Js
Express Js
- Express.js is a Node js web application server framework, which is specifically
designed for building single-page, multi-page, and hybrid web applications.
- Express gets installed via the Node Package Manager. This can be done by executing
the following line in the command line
If the current middleware function does not end the request-response cycle, it must call next() to
pass control to the next middleware function. Otherwise, the request will be left hanging.
Middlewares
Application-level middleware
- Bind application-level middleware to an instance of the app object by using the
app.use() and app.METHOD() functions
Router-level middleware
- Router-level middleware works in the same way as application-level middleware,
except it is bound to an instance of express.Router().
Error-handling middleware
- Error handling middleware in an application detect and capture multiple error
conditions and take appropriate remedial actions to either recover from those errors
or fail gracefully
- Error-handling middleware always takes four arguments. You must provide four
arguments to identify it as an error-handling middleware function.
Third-party middleware
- Use third-party middleware to add functionality to Express apps.
- Install the Node.js module for the required functionality, then load it in your app at
the application level or at the router level.
Exercise
Login
Logout
Access homepage
npm install pg
Pg examples
Pg examples
Pg examples
Exercise