Node js Fundamentals and Features
Read This on Github
⭐ Fundamentals and Features
Explain
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 (opens in a new tab)
Some Important Notes
- Suppose you make a file
app.js
and another fileindex.js
and you want to import theapp.js
file like this
export const name = "Subham Maity"
export const age = 20
import {name,age} from "./app.js"
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
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
}
const {name,age} = require("./app.js")
console.log(name,age)
Now if you run node index.js
then you will get the output like this
Subham Maity 20
- You can even create a function in
app.js
and export it and then import it inindex.js
and use it.
module.exports = {
add: (a,b) => a+b
}
const app = require("./app.js")
console.log(app.add(2,3))
Now if you run node index.js
then you will get the output like this
5
What is filter in Javascript ?
- If you have an array like this
const arr = [1,2,3,4,5,6,7,8,9,10]
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)
Now if you run node index.js
then you will get the output like this
[ 6, 7, 8, 9, 10 ]
- If you have an array like this
const arr1 = [1,2,3,4,4,4,5,6,7,8,9,10]
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)
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)