Node Flashcards
What’s the difference between NodeJS and Vanilla JS?
Node runs on the server while Vanilla JS runs on the browser. So the console for node is the terminal window while the console for JavaScript prints in the browsers dev tools console.
Node objects are global objects while JS makes window objects. So printing window in JS shows a large JS object with many properties while printing global in node shows a similar, but much smaller object.
Node has common core modules
CommonJS modules instead of ES6 modules, which means imports must be done with require instead of import. For example
const os = require(‘os’)
What are two node values always available to us for file paths?Is there another tool we can use for this?
__dirname
__filename
These give the full path to the directory and file name
import ‘path’ using require and use path.parse(__filename) to get all the related info dealing with the path names
How do you make your own imports and exports in NodeJS? How do you pull specific functions while importing?
Make a file with different functions, and at the end:
module.exports = { func1, func2, …}
Note: you can also exempt the above line and instead of declaring the functions with const func1 = …, you can just declare them like exports.func1 = … and it will automatically have them exported.
Then in another folder where you want to use those functions:
const functions = require(‘./functions file’)
Now we can use functions.func1() in that file.
We could do this instead:
const { func1, func2, … } = require(‘./functions file’)
Then we would just need to use func1() to call it.
How do you get your server to restart every time you save the server file?
npm install -g nodemon
npm install –save-dev nodemon
Then use “nodemon server.js” for example to start the server. It will restart whenever you save the file.