DEV Community

loading...
Cover image for Node.js 101 - part 1: What is Node.js?

Node.js 101 - part 1: What is Node.js?

ericchapman profile image Eric The Coder Updated on ・4 min read

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.

What is Node.JS?

Node.js is a free open source server environment that use Javascript runtime for server side web development.

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.

First Step

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
Enter fullscreen mode Exit fullscreen mode

Classic Hello World

We cannot learn a new tech without the classic Hello World first app.

Create a file name index.js

// index.js
console.log('Hello World')
Enter fullscreen mode Exit fullscreen mode

To execute that script. Use the command: node 'script name'

$~ node index.js
Hello World
Enter fullscreen mode Exit fullscreen mode

Use Node.js modules

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

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

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

Reading and Writing Files Synchronous

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!
Enter fullscreen mode Exit fullscreen mode

Now I will try to read the file content:
index.js

const fs = require('fs')

const info = fs.readFileSync('info.txt', 'utf-8')
console.log(info) // file content
Enter fullscreen mode Exit fullscreen mode

Write file

const text = `This is a 
multi lines 
text file`

// Write to file (will overwrite existing content)
fs.writeFileSync('info.txt', text)
Enter fullscreen mode Exit fullscreen mode

Synchronous vs Asynchronous

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.

Reading and Writing Files - Asynchronous

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')
Enter fullscreen mode Exit fullscreen mode

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.

Use your own module

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

module.exports = () => {
    console.log('Some code')
    return something
}
Enter fullscreen mode Exit fullscreen mode

Multiple export

// 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')
Enter fullscreen mode Exit fullscreen mode

Once you create the file, you can use it in your main file

const loadData = require('./loadData.js')

Enter fullscreen mode Exit fullscreen mode

Conclusion

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: Follow @justericchapman

Discussion (0)

Forem Open with the Forem app