DEV Community

Cover image for Fundamentals and Features of Node.js
Subham
Subham

Posted on

Fundamentals and Features of Node.js

Hi, I'm Subham Maity, a software engineer. I also enjoy teaching others how to code through my tutorials. I'm always eager to learn new things and share my knowledge with the community.

⚡ I recently wrote an article on Fundamentals and Features of Node.js and wanted to share it with you all. You can find the article on my website https://codexam.vercel.app/docs/node/node1 [Better View]

⚡ I also have a repository on GitHub where you can find all the code and projects related to this topic. You can find the repository at https://github.com/Subham-Maity/node-js-full-stack-tutorial

❗ For a more in-depth look at this topic, including a detailed table of contents, check out the complete tutorial on my GitHub Repo

If you want to stay updated with my latest projects and articles, you can follow me on:

For NodeJs you need to have a basic knowledge of Javascript. If you are new to Javascript then you can check out my tutorial on Javascript

Some Important Notes

  1. Suppose you make a file app.js and another file index.js and you want to import the app.js file like this
export const name = "Subham Maity"
export const age = 20
Enter fullscreen mode Exit fullscreen mode
import {name,age} from "./app.js"
Enter fullscreen mode Exit fullscreen mode

And in your terminal you run node index.js then you will get an error like this

SyntaxError: Cannot use import statement outside a module
Enter fullscreen mode Exit fullscreen mode

This is because NodeJs doesn't support the import and export syntax. So you have to use the module.exports and require syntax.

module.exports = {
    name: "Subham Maity",
    age: 20
}
Enter fullscreen mode Exit fullscreen mode
const {name,age} = require("./app.js")

console.log(name,age)
Enter fullscreen mode Exit fullscreen mode

Now if you run node index.js then you will get the output like this.

Subham Maity 20
Enter fullscreen mode Exit fullscreen mode
  1. You can even create a function in app.js and export it and then import it in index.js and use it.
module.exports = {
    add: (a,b) => a+b
}
Enter fullscreen mode Exit fullscreen mode
const app = require("./app.js")

console.log(app.add(2,3))
Enter fullscreen mode Exit fullscreen mode

Now if you run node index.js then you will get the output like this

5
Enter fullscreen mode Exit fullscreen mode

What is filter in JavaScript ?

  • If you have an array like this
const arr = [1,2,3,4,5,6,7,8,9,10]
Enter fullscreen mode Exit fullscreen mode

and you want to traverse through the array and get the elements which are greater than 5 then you can use the filter method.

const arr = [1,2,3,4,5,6,7,8,9,10]

const greaterThan5 = arr.filter((element) => element > 5)

console.log(greaterThan5)

Enter fullscreen mode Exit fullscreen mode

Now if you run node index.js then you will get the output like this.

[ 6, 7, 8, 9, 10 ]
Enter fullscreen mode Exit fullscreen mode
  • If you have an array like this
const arr1 = [1,2,3,4,4,4,5,6,7,8,9,10]
Enter fullscreen mode Exit fullscreen mode

and you want to traverse and find the value that is repeated the most then you can use the filter method.

const arr1 = [1,2,3,4,4,4,5,6,7,8,9,10]

const mostRepeated = arr1.filter((element) =>
{
    return element === 4
})

console.log(mostRepeated)

Enter fullscreen mode Exit fullscreen mode

Even you can do greater than 5 and less than 8

const arr1 = [1,2,3,4,4,4,5,6,7,8,9,10]

const mostRepeated = arr1.filter((element) =>
{
    return element > 5 && element < 8
})

console.log(mostRepeated)

Enter fullscreen mode Exit fullscreen mode

Top comments (0)