Setting Up Your First Node.js Application Step-by-Step

today i am here to give you a walkthrough of your first node.js app . Whether you use windows , mac or linux i will try to be OS neutral so anyone can follow along.
Install Node.Js
installing node is as easy as installing a game .
Just head to Node.Js .
You have two options
via command prompt

choose any LTS (long term support) version never go for current/latest because they are stable .
choose your os and copy the cmds and run step by step in your command prompt.
2. use GUI
if you are not cmd comfortable right now you can choose this method .
just below the cmds you will find this option .
choose your preference and download it . Execute the file .
Checking installation using terminal
in your terminal run node -v to see the version . if anything else comes (likely some error , then the download is not completed) .( if installing through cmd prompt , reopen the cmd for updated results.)
Understanding Node REPL
To instantly try this new installation , just type node in terminal and you will enter in Node REPL (Read-Eval-Print Loop) .
then you can write this line
console.log("hello World!")
you will see hello world printed in your console .
ignore the undefined below it , it is not an error .
Here you can play with javascript .
But it shows you output for every line . but for building app we dont want line by line execution .
Creating first JS file
head to any IDE you want . I prefer VS code .
create a file with .js extension ( it important ) .
Execute the file using node.
now previously we installed node on our machine , that will help us to execute this code . for that we have to use
node filename.js // if executing in same folder as file .
or
node filepath
Creating hello world Server
lets now build a server that will allow you to call an api and that will give you hello world.
for that we have to initialize node project .
check npm -v in terminal .
then in your terminal run npm init -y .
you will see this package.json in your directory .
the -y in npm init -y is for default options ie. yes to all .
if you run only npm init you will be asked for all the information in the package.json step by step .
if you want , you can edit this information any time.
after initialising this , we have to install a package that in order to create a server .
run npm install express .
when installed successfully you will see express in you dependencies.
if you want to read about express you can visit express.
in your index.js file write the below code
const express = require('express');
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
and then in you terminal run the index file using node index.js.
visit page -> http://localhost:3000 on your browser and you will see this .
voila you have written your first server using express.




