a guest Oct 22nd, 2019 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- # What is Nodejs
- NodeJs is an open source runtime environment. Runtime simply means something that is executable in it's own environment.
- Node is not a convention based platform where everything is thought through such as the database, sessions, authentication, etc.
- For example PHP, Ruby on Rails etc. **NodeJs has no idea or does not know which database you are going to hook on to.**
- Node is not recommmended for high CPU intensive operations like *reading the file system asynchronously, or machine learning or
- large arithemetic*. Node is good when it comes to concurrency.
- In summary, node is not a single bullet for everything.
- ## What can I create with NodeJs
- - Great for tooling
- - Great for creating APIs (REST, Realtime, Sockets) because of it's high concurrency. It can handle a lot of request at the same time.
- - Building CDNs
- In summary, node can do pretty much anything because node is on everything now.
- ## Installing NodeJs
- The advantage of NVM over node is that it is recommended and allows you to run many versions of node whenever.
- Moreover, nodejs causes permission errors
- ## Running Nodejs code
- **Interactive REPL:** This is mainly for playing around. It stands for READ, EVALUATE, PRINT, LOOP. It is an interactive environment for writing code and it is use for testing things out like the console in the browser. Either than that, it is not used for anything in terms of building full fledge applications.
- So the REPL is great for trying things out. Think of the console in the browser. To use REPL just tpe the node command with no args and begin
- **CLI Executable:** Used for everything else. It is used to execute your node apps but now you pass the path to the file you want to run.
- ## Globals in Nodejs
- Node provides you with helpful globals such as:
- **process:** has information about the environment the program is running in such as the path, OS version, environment variable etc.
- **require:** function to find and use modules in the curernt module.
- **__dirname:** the current directory path.
- **module:** is the sibling of require where modules are made consumable. Simply put, it is a way of exporting a module so that other modules can require them.
- **global:** like window, it is the "global object". Almost **never use this**
- **__filename:** gives you the current filename
- ## What are modules?
- Modules are basically encapsulated code. Nodejs uses commonJs for it's module system. Some of the other module systems out there are:
- -ESM(ecmascript modules)
- -AMD(it's outdated)
- ## Creating Node Modules
- Creating modules in node in just regular code that needs to be exported.
- ## NPX
- First of all, npx comes with node. Npx allows you to run locally installed modules on the fly (eg. jest) rather than typing out the exact location of the module let's say >> node modules/jest
- Also, incase the module is not installed, npx installs the package before running
RAW Paste Data