Node.js 101 - part 1: What is Node.js?
Tags: javascript, node, beginners, tutorial
Published: 2021-03-07 15:35:14

---
title: Node.js 101 - part 1: What is Node.js?
published: true
description: 
tags: javascript, node, beginner, tutorial
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4anecy5mdl4pho8w7519.jpg
series: Node.js 101
---

I strongly recommend learning javascript first. Here a series of post I did on Dev.to: https://dev.to/rickavmaniac/javascript-my-learning-journey-part-1-what-is-javascript-role-and-use-case-49a3

I am now ready to continue my Node.js learning journey :)

Click follow if you want to miss nothing. I will publish here on Dev.to what I learn everyday.

Without further ado here is a summary of my notes for my last day.

<h2>What is Node.JS?</h2>
Node.js is a free open source server environment that can run Javascript code outside of the browser. With Node you can create server side web application in Javascript.

Node.js runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)

With Node.js we can build fast and highly scalable web application

Using Node.js also mean we can use Javascript across the entire stack. That mean fast and efficient development.

Node.js have a huge library of packages that are ready to use and will save ton of times. Those libraries are manage by NPM (Node Package Manager)

Node.js can be install directly from there web site: https://nodejs.org/en/

Detail documentation is also available on Node.js web site: https://nodejs.org/en/docs/

One particularity of Node.js is that he use asynchronous programming vs synchronous programming found on many server-side programming language! 

Node.js is single-treaded, base on event, non blocking I/O model. More on that later.

<h2>First Step</h2>
Once installed, you can run Node.js repl by typing node in your terminal.

Here a exemple of a simple console log
```
$~ node

> const name = 'Mike'
> console.log(name)
> Mike
> .exit
```

<h2>Classic Hello World</h2>
We cannot learn a new tech without the classic Hello World first app.

Create a file name index.js
```js
// index.js
console.log('Hello World')
```

To execute that script. Use the command: node 'script name'
```
$~ node index.js
Hello World
```

<h2>Use Node.js modules</h2>
Like mention in introduction Node.js have many pre-build functionality's/module that we can use to easily accomplish many tasks.

For example if we want to access the file system we can use a module name: fs

```js
// To reference a module use require() method
const fs = require('fs')
// The fs variable now reference the Node.js module 'fs'
```

If you want to know all available modules you can consult the documentations: https://nodejs.org/dist/latest-v15.x/docs/api/

<h2>Reading and Writing Files Synchronous</h2>
First let's create a file name info.txt and put some ramdom text.

info.txt
```
Hi, this is info.text file with some random text info. I will try to read and write to this file with Node.js. Good luck to me!
```

Now I will try to read the file content:
index.js
```js
const fs = require('fs')

const info = fs.readFileSync('info.txt', 'utf-8')
console.log(info) // file content
```
Write file
```js
const text = `This is a 
multi lines 
text file`

// Write to file (will overwrite existing content)
fs.writeFileSync('info.txt', text)
```

<h2>Synchronous vs Asynchronous</h2>
The Read and Write file method we use was synchronous method. That mean Node.js will wait for the method to finish before executing the next line of code. That's why synchronous code is also call blocking code.

In Asynchronous mode, Node.js will not wait for the response and will continue the code execution. That's call non blocking code. 

Since Node.js is single thread. If you run synchronous code, you will block all other users code execution until the synchronous code finish. Thats could lead to massive performance hit.

That's why Node.js use mostly asynchronous methods. Asynchronous methods will run in the background and will not block the main single thread.

<h2>Reading and Writing Files - Asynchronous</h2>

```js
const fs = require('fs')

// This method is async, The code execution will not block
// The last parameter is a callback function. 
// This function will be execute only when the readFile background execution is finish.
const info = fs.readFile('info.txt', 'utf-8', (err, data) => { 
    console.log(data)
})
console.log('File was read')
```
If you execute this code you will notice that the line 'File was read' will be the first line to show in the terminal. And that's logic since the read file method is async. 

The readFile method will run in the background to allow the code execution not to stop. Only when the readFile method is finish the callback function (console.log) will be execute.

<h2>Use your own module</h2>
Not all your code need to be in the same file. Sometime it's good practice to separate repeated code in a module that can be callback every time we need it.

To create a custom module. Just create a new file name whatever you want and prefix your function with module.export. 

Example: loadData.js
```js
module.exports = () => {
    console.log('Some code')
    return something
}
```

Multiple export
```js
// For example in a module, export function
exports.displayFullName = () => console.log('full name')
exports.displayNickName = () => console.log('nick name')

// In another module use those export
const { fullName, nickName } = require('./name_functions')
```

Once you create the file, you can use it in your main file
```js
const loadData = require('./loadData.js')

```


<h2>Conclusion</h2>
Cool Javascript on the server :-) We just enter into whole new world of possibilities. 

We will come back later to async and callback functions. For now next let continue our Node.js exploration by creating our first web server. See you tomorrow...

Follow me on Twitter: <a href="https://twitter.com/justericchapman?ref_src=twsrc%5Etfw" class="twitter-follow-button" data-show-count="false">Follow @justericchapman</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>