Skip to content

Commit

Permalink
Update all the Basic section docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Aahil13 committed Apr 16, 2024
1 parent 9847706 commit f0b3b8f
Show file tree
Hide file tree
Showing 8 changed files with 621 additions and 8 deletions.
79 changes: 78 additions & 1 deletion src/docs/Basics/Email.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,81 @@ category:
- Docs-Basics

footer: false
---
---

In this tutorial, we'll explore how to use the Nodemailer module to send emails from your Node.js server.

## The Nodemailer Module

The Nodemailer module simplifies the process of sending emails from your computer. You can easily download and install the Nodemailer module using npm:

```bash
npm install nodemailer
```

After installing the Nodemailer module, you can include it in any application:

```javascript
var nodemailer = require('nodemailer');
```

## Sending an Email

Now that you've installed the Nodemailer module, you're ready to send emails from your server. Let's walk through an example of sending an email using your Gmail account:

```javascript
var nodemailer = require('nodemailer');

var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'yourpassword'
}
});

var mailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: 'Sending Email using Node.js',
text: 'That was easy!'
};

transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
```

And that's it! Your server is now capable of sending emails.

## Multiple Receivers

To send an email to multiple receivers, simply add them to the `to` property of the `mailOptions` object, separated by commas:

```javascript
var mailOptions = {
from: '[email protected]',
to: '[email protected], [email protected]',
subject: 'Sending Email using Node.js',
text: 'That was easy!'
}
```

## Send HTML

If you want to send HTML-formatted text in your email, use the `html` property instead of the `text` property:

```javascript
var mailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: 'Sending Email using Node.js',
html: '<h1>Welcome</h1><p>That was easy!</p>'
}
```

That's all you need to know to start sending emails from your Node.js server using the Nodemailer module.
49 changes: 48 additions & 1 deletion src/docs/Basics/Events.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,51 @@ category:
- Docs-Basics

footer: false
---
---

In this tutorial, we'll explore how Node.js is ideal for building event-driven applications.

## Events in Node.js

Node.js is well-suited for event-driven applications, where every action on a computer is treated as an event. For example, when a connection is made or a file is opened, these are considered events.

Objects in Node.js can fire events, such as the `readStream` object which fires events when opening and closing a file. Let's see an example:

```javascript
var fs = require('fs');
var rs = fs.createReadStream('./demofile.txt');
rs.on('open', function () {
console.log('The file is open');
});
```

## Events Module

Node.js provides a built-in module called "Events" that allows you to create, fire, and listen for your own events.

To include the built-in Events module, use the `require()` method. Additionally, all event properties and methods are instances of an EventEmitter object. To access these properties and methods, create an EventEmitter object:

```javascript
var events = require('events');
var eventEmitter = new events.EventEmitter();
```

## The EventEmitter Object

You can assign event handlers to your own events using the EventEmitter object. In the following example, we've created a function that will be executed when a "scream" event is fired. To fire an event, use the `emit()` method.

```javascript
var events = require('events');
var eventEmitter = new events.EventEmitter();

// Create an event handler:
var myEventHandler = function () {
console.log('I hear a scream!');
}

// Assign the event handler to an event:
eventEmitter.on('scream', myEventHandler);

// Fire the 'scream' event:
eventEmitter.emit('scream');
```
106 changes: 105 additions & 1 deletion src/docs/Basics/File System.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,108 @@ category:
- Docs-Basics

footer: false
---
---

In this tutorial, we'll explore how to work with the file system on your computer using Node.js.

## Node.js as a File Server

The Node.js file system module allows you to perform various operations on files, such as reading, creating, updating, deleting, and renaming files. To include the File System module, use the `require()` method:

```javascript
var fs = require('fs');
```

## Read Files

The `fs.readFile()` method is used to read files on your computer. Let's see an example of reading an HTML file:

```javascript
var http = require('http');
var fs = require('fs');

http.createServer(function (req, res) {
fs.readFile('demofile1.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(8080);
```

## Create Files

The File System module provides methods for creating new files, such as `fs.appendFile()`, `fs.open()`, and `fs.writeFile()`. Let's see examples of creating new files:

```javascript
var fs = require('fs');

fs.appendFile('mynewfile1.txt', 'Hello content!', function (err) {
if (err) throw err;
console.log('Saved!');
});
```

```javascript
var fs = require('fs');

fs.open('mynewfile2.txt', 'w', function (err, file) {
if (err) throw err;
console.log('Saved!');
});
```

```javascript
var fs = require('fs');

fs.writeFile('mynewfile3.txt', 'Hello content!', function (err) {
if (err) throw err;
console.log('Saved!');
});
```

## Update and Delete Files

The File System module also provides methods for updating and deleting files, such as `fs.appendFile()`, `fs.writeFile()`, and `fs.unlink()`. Let's see examples of updating and deleting files:

```javascript
var fs = require('fs');

fs.appendFile('mynewfile1.txt', ' This is my text.', function (err) {
if (err) throw err;
console.log('Updated!');
});
```

```javascript
var fs = require('fs');

fs.writeFile('mynewfile3.txt', 'This is my text', function (err) {
if (err) throw err;
console.log('Replaced!');
});
```

```javascript
var fs = require('fs');

fs.unlink('mynewfile2.txt', function (err) {
if (err) throw err;
console.log('File deleted!');
});
```

## Rename Files

To rename a file, use the `fs.rename()` method:

```javascript
var fs = require('fs');

fs.rename('mynewfile1.txt', 'myrenamedfile.txt', function (err) {
if (err) throw err;
console.log('File Renamed!');
});
```

You can perform various file operations easily using these methods 📁
74 changes: 73 additions & 1 deletion src/docs/Basics/HTTP Modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,76 @@ category:
- Docs-Basics

footer: false
---
---

Let's explore how Node.js utilizes the built-in HTTP module to transfer data over the Hyper Text Transfer Protocol (HTTP).

## The Built-in HTTP Module

Node.js provides a built-in module called HTTP, which allows you to create HTTP servers and handle HTTP requests and responses. To include the HTTP module, use the `require()` method:

```javascript
var http = require('http');
```

## Node.js as a Web Server

With the HTTP module, Node.js can act as a web server by creating an HTTP server that listens to server ports and responds to client requests. Use the `createServer()` method to create an HTTP server:

```javascript
var http = require('http');

// Create a server object:
http.createServer(function (req, res) {
res.write('Hello World!'); // Write a response to the client
res.end(); // End the response
}).listen(8080); // The server object listens on port 8080
```

The function passed into the `http.createServer()` method will be executed when someone tries to access the computer on port 8080.

## Add an HTTP Header

To display the response from the HTTP server as HTML, include an HTTP header with the correct content type:

```javascript
var http = require('http');

http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Hello World!');
res.end();
}).listen(8080);
```

The first argument of the `res.writeHead()` method is the status code, where 200 means that all is OK, and the second argument is an object containing the response headers.

## Read the Query String

The `req` argument of the function passed into `http.createServer()` represents the request from the client as an object (`http.IncomingMessage` object). It has a property called "url" which holds the part of the URL that comes after the domain name:

```javascript
var http = require('http');

http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(req.url);
res.end();
}).listen(8080);
```

## Split the Query String

You can easily split the query string into readable parts using the built-in URL module:

```javascript
var http = require('http');
var url = require('url');

http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
var q = url.parse(req.url, true).query;
var txt = q.year + " " + q.month;
res.end(txt);
}).listen(8080);
```
Loading

0 comments on commit f0b3b8f

Please sign in to comment.