Refs
What is a Javascript Module ?
Just a javascript file which can import functionality from other and export its own functionality.
Just a javascript file which can import functionality from other and export its own functionality.
The industry-standard protocol for authorization.
This specification and its extensions are being developed within the IETF OAuth Working Group.
(THIS BLOG IS STILL IN PROGRESS)
A strongly typed programming language that builds on JavaScript.
Smaller, more expressive, more robust.
VS Epxress
The key difference between Koa and Express is how they handle middleware. Express includes routing and templates in the application framework. Koa, on the other hand, requires modules for these features, therefore making it more modular or customizable.
A material-design UI framework for UI.
vue-cli
created project, type vue add vuetify
in project root folder.<el-config-provider>
for configuration to work.<el-config-provider>
will pass to all sub-components. See available propertiesExample:
A thread that enables node.js to execute JavaScript in parallel. Useful to handle CPU intensive jobs.
Create a worker file => Make a promise in caller file => Define on message/error/exit hooks
worker.js file:
1 | const { workerData, parentPort } = require('worker_threads') |
index.js file:
1 | const { Worker } = require('worker_threads') |
Key points:
workerData
to receive data from caller and parentPort
to post data to caller.parentPort.postMessage
to send data to caller and caller use on('message')
to receive.Single threaded node.js will block on a time-consuming request.
Example:
1 | const http = require('http'); |
Call localhost:3000/compute
will block
Pros:
Use child_process.fork
to create new process.
Main.js:
1 | const http = require('http'); |
compute.js:
1 | const computation = () => { |
cluster
can create worker process in a single file.
Example:
1 | const http = require('http'); |
Running code above, we got:
1 | Master process id is 18428, cpu number 4 |
We can find these process in task manager:
Now send a request in browser, and id of one of the four server process is returned:
Try refresh many times and different pids may return(It depends, maybe one unlucky process shoulders all the workload).
Now kill one the worker 1460
in task manger and we got:
1 | worker process died,id 1460 |
Refresh the browser and result is another pid other than 1460:
You see, now our server is much more robust than before. We got four worker process, killing one of them and there are still three working.
Cluster calls the same fork
method from child_process
module under the hood. Cluster is a master-slave model, where master manages and schedules slaves.
Why no Error: EADDRINUSE
when multiple processes listens on the same port?
The child processes aren’t listening to the same port. Incoming socket connections to the master process are being delegated to the child processes. There’s special handling for clustered process in server.listen()
, it calls a method named listenInCluster()
in some circumstances. See explanation here
cluster
worker threads
SharedArrayBuffer
)