NodeJS
REPL Node.js --Global Objects / util - Global Objects / util----File System / HTTP-----express,mocha,npm
Last updated
Was this helpful?
REPL Node.js --Global Objects / util - Global Objects / util----File System / HTTP-----express,mocha,npm
Last updated
Was this helpful?
Node.JS is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code server-side.
core is async I/O
Use async I/O not multi thread
like search: db.query('SELECT * from some_table', function(res) { res.output(); });
callback as second parameter. async -> event loop, solve the block
everything is event loop
Usage: node [options] [ -e script | script.js ] [arguments]
e.g.: node debug script.js [arguments]
allows Node.js to perform non-blocking I/O operations
the kernel tells Node.js so that the appropriate callback may be added to the poll queue to eventually be executed. We'll explain this in further detail later in this topic.
if (poll queue is empty), {
if (has setImmediate), {poll will stop and go to check run this setImmediate.}
else: run callback, set timer
} else {
traverse queue, until is empty
}
Micro Task vs Macro Task:
macro-task: setTimeout、setInterval、 setImmediate、script、 I/O
micro-task : process.nextTick、new Promise().then
Browser:timer1=>promise1=>timer2=>promise2
node newest: timer1=>promise1=>timer2=>promise2
before node10 timer1=>promise1=>timer2=>promise2
REPL stands for (READ, EVAL, PRINT, LOOP). NodeJS comes with bundled REPL environment. This allows for the easy creation of CLI (Command Line Interface) applications.
.break
- When in the process of inputting a multi-line expression, entering the .break
command (or pressing the <ctrl>-C
key combination) will abort further input or processing of that expression.
.clear
- Resets the REPL context
to an empty object and clears any multi-line expression currently being input.
.exit
- Close the I/O stream, causing the REPL to exit.
.help
- Show this list of special commands.
.save
- Save the current REPL session to a file: > .save ./file/to/save.js
.load
- Load a file into the current REPL session. > .load ./file/to/load.js
.editor
- Enter editor mode (<ctrl>-D
to finish, <ctrl>-C
to cancel).
Express.JS is a web application framework for Node.JS. It is designed for building web applications and APIs.
Single Threaded Event Loop Model
Using Passport.js. Passport is authentication middleware for NodeJS. Passport can be easily dropped into any Express-based web applications. It's a comprehensive set of strategies supports authentication using a username and password, Facebook, Twitter.
JavaScript as the programming language
Asynchronous events
Real-time web apps
NPM package manager
Data sharing
Event driven architecture
Default http, https module
Request
Axios
SuperAgent
Got
NodeJS is a single thread process. It does not expose child threads and thread management methods to the developer. Technically, NodeJS does spawn child threads for certain tasks such as asynchronous I/O, but these run behind the scenes and do not execute any application JavaScript code, nor block the main event loop. If threading support is desired in a NodeJS application, there are tools available to enable it, such as the ChildProcess
module.
Since NodeJS is by default a single thread application, it will run on a single processor core and will not take full advantages of multiple core resources. However, NodeJS provides support for deployment on multiple-core systems, to take greater advantage of the hardware. The Cluster
module is one of the core NodeJS modules and it allows running multiple NodeJS worker processes that will share the same port.
To include the File System module, use the require()
method.
Read files - fs.readFile()
Create files - fs.appendFile(), fs.open(), fs.writeFile()
Update files - fs.appendFile(), fs.writeFile()
Delete files - fs.unlink()
Rename files - fs.rename()
Upload files
The util
module is primarily designed to support the needs of Node.js' own internal APIs. However, many of the utilities are useful for application and module developers as well. It can be accessed using:
The repl
module exports the class. While running, instances of will accept individual lines of user input, evaluate those according to a user-defined evaluation function, then output the result.