So let's first learn what is node js and what is it doing along with javascript.
What is Node.js?
When learning javascript we usually write code inside the console provided by the browser or create an HTML page then attach that script.js file to html and run it.
When developing a bigger application and adding backend services will create pain in the console every time you debug and see the output . . . .
So node.js is the solution to set up the dev environment.
JavaScript was only able to run on a browser, but Node.js allows JavaScript to run on a server. You can call Node.js a runtime, a platform, or an environment that allowed JavaScript to run on a server. Which allowed apps to be entirely written in JavaScript.
What you will learn or maybe you already know(revise) is
Use Node.js and NPM to create an application that prints "Hello World"
Step 1 : Installing Node.js
Installation of node.js
link :
https://nodejs.org/en/
download and setup node.js in your fav IDE
Step 2: Initializing node package
Now check the nodejs version by firing up this command
$node -v
It will display a version installed on your machine. Now we will initialise an npm package
$npm init
This will ask for metadata about the project you are creating and follow along with the prompt. This will create a package.json file
Every Node Package has package.json. It holds all the metadata of the project. It's like the ID of the project
Let's install express using npm. npm is a node package manager
$ npm install express
This should have created package-lock.json
and node_modules
directory. Now, we can use Express in our application!
Express is a good example of a package that wouldn't be possible without Node.js allowing JavaScript to run on a server. Express provides an easy way to set up integral parts of web apps like routing and handling HTTP requests.
Step 3: Creating an Entry point ==> index.js file
Create a new file name it index.js and then write the code below
const express = require('express') // importing express
const app = express() //giving express features to app variable which is constant
const port = 3000 //choosing a port
app.get('/', (req, res) => { //creating a http request
res.send('Hello World!') //sending a response message
})
app.listen(port, () => { // displys the respone on the port
console.log(`Your app is live at ${port}`)
})
run the index.js file to see the output
$ node index.js
Check for the app on the select port number using localhost: port number. By now you might be seeing hello world.
To run an HTML page using Node.js there is a minor change to be made.
Imagine you have already created an HTML page let's display using nodejs
const path = require('path'); //import path lib
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'folder_name/helloworld.html'));
})
app.listen(port, () => {
console.log(`Your app is live at ${port}`)
})
That it save and run it again. This will display the web page developed. If in case you have encountered any errors google it.
I hope you enjoyed it as much as I did create this project. Start messing up with the project.
Feel free to share feedback and show your appreciation
Written by Rajender Reddy B