0% found this document useful (0 votes)
9 views

nodejs_notes

Node.js is an open-source JavaScript runtime environment that enables server-side application development using a non-blocking, event-driven architecture. It allows for scalability and efficiency through its single-threaded event loop and has a large ecosystem supported by NPM. The document also covers installation, basic usage with Express.js, and database interaction using MongoDB with Mongoose.

Uploaded by

abhi966512
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

nodejs_notes

Node.js is an open-source JavaScript runtime environment that enables server-side application development using a non-blocking, event-driven architecture. It allows for scalability and efficiency through its single-threaded event loop and has a large ecosystem supported by NPM. The document also covers installation, basic usage with Express.js, and database interaction using MongoDB with Mongoose.

Uploaded by

abhi966512
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

# Comprehensive Notes on Node.

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.

### Why Use Node.js?


- Single Programming Language: Uses JavaScript for frontend & backend development.
- Asynchronous and Event-Driven: Non-blocking I/O operations enable scalability.
- Lightweight and Efficient: Uses a single-threaded event loop instead of traditional multi-threaded servers.
- Large Ecosystem: NPM (Node Package Manager) provides thousands of reusable libraries.

## 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...');
```

### Single-Threaded but Highly Scalable


- Unlike traditional multi-threaded servers, Node.js uses a single-threaded event loop.
- Uses worker threads and clustering for CPU-intensive tasks.

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
```

## 4. Express.js - Web Framework for Node.js


### Basic Express.js Server
```js
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello, Express!');
});
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});
```

## 5. Working with Databases in Node.js


### Using MongoDB with Mongoose
```js
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/mydb', { useNewUrlParser: true, useUnifiedTopology: true
const UserSchema = new mongoose.Schema({ name: String, age: Number });
const User = mongoose.model('User', UserSchema);
User.create({ name: 'John Doe', age: 30 });
```

## Conclusion
Node.js is a powerful JavaScript runtime for efficient, scalable, and high-performance server-side applicatio

You might also like