Node.js is an open-source Javascript runtime environment that can run on OS. It helps us to execute JS code outside of a web browser with the help of V8 JS Engine. Most use cases are writing command line tools and server-side scripting.
To use Node.js in our machine we should install it. For the best, it’s always better to run the recommended version.
Also in some cases, we need to work on the different node versions and we can travel between the node versions by using NVM - Node Version Manager. NVM is an open-source node version manager that allows us to change the node version via the command line quickly. To install:
Node.js comes with lots of different APIs that help us to use in certain scenarios and to use the module, we simply need to call them in our script file by using require keyword.
const fs = require('fs') // fs is the file system object that we can call from node.js to work on the file system
const myName = 'Dodo'
fs.writeFile('exampleFileName.txt', 'My Name is: ' + myName, (err) => {
if(err){
console.log(err)
return
}
console.log('File created and the name written')
}) // We just created a txt file which name is `exampleFileName` and we write `My Name is: Dodo' inside the file by using the node.
There are lots of different modules that we can use within node.js but one of the most popular modules come with node.js is http module. It helps us to send requests and responses.
As a frontend developer trying to be a full stack now, I am mostly using the
httpmodule to create my own APIs to build full stack applications.
const http = require('http') // It requires the http module to handle the http requests
const server = http.createServer((req, res) => {
console.log('Incoming Request')
console.log(req.method, req.url)
res.end('Success')
}) //This creates a local server in our machine
const host = 'localhost'
const port = 5005
server.listen(port, host, () => {
console.log(`Server is running on <http://$>{host}:${port}`)
}) // We listen to the server from our local environment and if we go to localhost:5000 we'll see `Success` in the browser.
One important thing is if you are gonna run a local server by using node, make sure that the port you are going to use not being used by something else, otherwise it’ll create conflict and you’ll not able to run your server locally.
<aside> 💡 On Mac OS, port 5000 is used by AirPlay, if you try to run your node server on port 5000, you’ll see access to localhost was denied screen down below.

</aside>