An open API service indexing awesome lists of open source software.

https://github.com/mthirumalai2905/node

Node.js is a runtime environment that allows you to execute JavaScript code outside of a web browser.
https://github.com/mthirumalai2905/node

expressjs mongodb nodejs

Last synced: 2 months ago
JSON representation

Node.js is a runtime environment that allows you to execute JavaScript code outside of a web browser.

Awesome Lists containing this project

README

        

## Node.js
``Node.js is a javascript runtime Environment Built upon Chrome's V8 Engine``

## History of Nodejs
```txt
In 2009 an idea came in the mind of Ryan Dahl(Google Engineer)
that why not run javascript
outside browser, so he took v8 engine and embedded in a c++ program
and called it Node.exe later then came to be known as Node.js
```
```txt
In 1995 Netscape written a program called Netscape Enterprise server
for a runtime javascript but it didnt work
```

## When to Use Nodejs
1) I/O Bound
2) Data Streaming Applications
3) Real time chat Applications

## Node JS REPL

The `REPL` feature of Node is very useful in experimenting with Node.js codes and to debug javascript codes.

**R** - `Read` user's input parses the input into Javascript data-structure,and stores in memory

**E** - `Eval` Takes and evaluates the data structure

**P** - `Print` Prints the result

**L** - `Loops` the above command until the user presses ctrl-c twice

```bash
node
Welcome to Node.js v20.10.0.
Type ".help" for more information.
> 5 + 3
8
> _ + 2
10
>
```

```bash
// Create files thorugh terminal
type > index.js

//Read the inside file
type index.js
```
**In REPL press Tab twice**
```bash
AbortController
AbortSignal
AggregateError
Array
.
.
.
.
....

constructor

> setTimeout
[Function: setTimeout] {
[Symbol(nodejs.util.promisify.custom)]: [Getter]
}
> clearTimeout
[Function: clearTimeout]
> clearTimeout()
undefined
> fs
......
```
## Modules in Node JS
**File System**
```javascript
const fs = require('fs');

//creating a new file
fs.writeFileSync("read.txt", "Bench Press 100 kilos withing 3 months")

//It overides the previous data present in the file
fs.writeFileSync("read.txt", "Bench Press 100 kilos withing 3 months will this overide the previous data")

//Add datas to the existing file
fs.appendFileSync("read.txt", " \nLol am already benching 100 wbu ?")

//Reading the existing data
const buf_data = fs.readFileSync("read.txt", "utf-8");//Takes 2 arguments file name and encoding-type
console.log(buf_data)

//Rename File
fs.renameSync("read.txt", "readandwrite.txt");

//Buffer - is an additional data type available in node to store binary datas
```

## CRUD OPERATIONS USING FS MODULES
```javascript
const fs = require('fs');

try {
// Create the folder
try {
fs.mkdirSync('folder');
console.log("Folder created successfully");
} catch (err) {
if (err.code === 'EEXIST') {
console.log("Folder already exists");
} else {
throw err; // Throw error if it's not 'EEXIST'
}
}

// Create a new file
fs.writeFileSync('folder/new.txt', 'Node creating file within the folder');
console.log("File created successfully");

// Append data to the file
fs.appendFileSync('folder/new.txt', '\nappending some data');
console.log("Data appended to the file successfully");

// Read data from file
const data = fs.readFileSync('folder/new.txt', 'utf8');
console.log("Data read from file:", data);

// Copy data from one file to another
const sourceData = fs.readFileSync('folder/new.txt', 'utf8');
fs.writeFileSync('folder/blah.txt', sourceData);
console.log("Data copied to another file successfully.");

// Rename the file
fs.renameSync('folder/new.txt', 'folder/renamed.txt');
console.log("File renamed successfully.");

// Delete the file
fs.unlinkSync('folder/blah.txt');
console.log("File deleted successfully.");

// Delete the folder
fs.rmdirSync('folder');
console.log("Folder deleted successfully.");

} catch (err) {
console.error("Error occurred:", err);
}

```
## Importing and Exporting module

```javascript
// The file which will be exported

const add(a,b){
return a + b
}

const sub(a,b){
return a - b
}

module.exports = {add, sub}
```

```javascript
// The file which will be importing that module
const {add,sub} = require('./file_name');

console.log(add(5,5));
console.log(sub(10,5));
```

```javascript

//console.log(__dirname);: This will print out the absolute path of the directory where the //current JavaScript file resides.

//console.log(__filename);: This will print out the absolute path of the current JavaScript //file itself.
console.log(__filename);
console.log(__dirname);
```

## Customizing the views directory for template engines(hbs,ejs,pug)

```javascript
const templatePath = path.join(__dirname, "../templates");

app.set("views", templatePath);
```

## Partials

```javascript
// Similar to components structure used in React

{{>header}}
```