NODEJS FUNDAMENTALS
Andrew University
Computer Science Department
Copyright By PowCoder代写 加微信 powcoder
STARTING A NODE PROGRAM
> node [filename].js
THINGS CAN HALT/END > ERRORS/EXCEPTIONS
Any unhandled exception in your code will halt (stop) your Node process.
Need your Node up and running, never going down (In Production)? try PM2
https://pm2.keymetrics.io
> MODULES Open Source.
Modules developed from other companies or people
Core modules come with the installation of NodeJS.
Node¡¯s module system is patterned after the CommonJS system
The project was started by Mozilla engineer in January 2009
COMMONJS VS.
ES6 modules use ¡®export¡¯ and ¡®import¡¯.
CommonJS uses ¡®module.exports¡¯ and ¡®require()¡¯
> COMMONJS
/modules/uppercase.js
export.uppercase = (str) => { return str.toUpperCase();
const uc = require(¡®modules/uppercase.js¡¯); uc.uppercase(¡°welcome¡±);
Use the require() function
Node will check to see if the module has been cached.
Node caches your module the 1st time it was used (loaded)
MODULES const express =
HOW NODE FINDS YOUR MODULES
require(¡®express ¡¯);
NODE > ES6 MODULES
ECMAScript modules are the official standard format to package JavaScript code
PICK ONE: DON¡¯T USE BOTH
You can use CommonJS & ES6 Modules in the same Node application
It is a hassle to work with. Just pick and stay with one module loading system.
REQUIRE() WITH ES6 MODULES?
Using require to load an ES module is not supported because ES modules have asynchronous execution
READ THE DOCS: DIFFERENCES BETWEEN ES6 MODULES
AND COMMONJS
https://nodejs.org/api/esm.html#esm_dif ferences_between_es_modules_and_co mmonjs
Why mixing module loading systems is a hassle…
IMPORT SPECIFIERS
ENABLING ES6 MODULES
The default is CommonJS. If you want to use ES6 Modules you need to indicate it.
2 Common approaches:
a. Module files end with .mjs (¡°module javascript¡±)
b. Package.json > ¡°type¡±: ¡°module¡±
THE NODE CONSOLE (REPL)
Read-Eval-Print-Loop
Using this shell program, we can execute pretty much any Node.js/JavaScript code
THE NODE CONSOLE (REPL)
Another example
medium.com/trabe/mastering-the-node-js-repl-part-1- 156ca2ee886a
nodejs.org/api/repl.html#repl_repl
THE NODE CONSOLE (REPL)
More for you to read…
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com