nodejs_notes
nodejs_notes
js
## 1. Introduction to Node.js
### What is Node.js?
- Node.js is an open-source, cross-platform JavaScript runtime environment.
- It executes JavaScript code outside the browser, making it ideal for server-side applications.
- Built on Google Chrome's V8 JavaScript engine, it compiles JavaScript to machine code for fast execution
- Node.js follows a non-blocking, event-driven architecture, making it highly efficient.
## 2. Features of Node.js
### Event-Driven and Non-Blocking I/O
- Node.js follows an event-driven architecture and uses the libuv library for async operations.
Example:
```js
const fs = require('fs');
fs.readFile('example.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
});
console.log('Reading file...');
```
Example:
```js
const { Worker } = require('worker_threads');
const worker = new Worker('./worker.js');
worker.on('message', message => console.log(`Received: ${message}`));
```
## 3. Installation of Node.js
### Installing Node.js and NPM
1. Download from [https://nodejs.org](https://nodejs.org)
2. Check Installation:
```sh
node -v # Check Node.js version
npm -v # Check npm version
```
3. Run a JavaScript File Using Node.js:
```sh
node filename.js
```
## Conclusion
Node.js is a powerful JavaScript runtime for efficient, scalable, and high-performance server-side applicatio