Introduction
So far all the Javascript, HTML and CSS we have learnt is part of front end, i.e the part the user interacts with. But to make a fully working web app we need what we call a "backend". It has three parts - Database, Server and Application.
Database is the storage part where we can store large amounts of data for a long time.
A server is basically a computer that is ready all the time to serve you listen to user requests and reply with html and css that the browser can use to render a web page.
Application is the logic that decides wt we do with certain types of requests.
We can write back end code in pretty much any language including ruby, php, python and JavaScript.
Here I will be using Javascript with node js and express js.
Node JS
JavaScript is made to be a language that runs in a browser, but for us to use it as the backend we need it to run on the server.
Node JS uses the V8 chrome engine to basically create an environment to run JavaScript outside the browser.
We will have to install node js on the server meant to run the code, we can use npm (a packet manager) to install dependencies/packages or frameworks as needed for the code.
We'll learn basics of node JS using a simple project - a QR code generator.
QR-Code Generator
import inquirer from "inquirer";
import qr from "qr-image";
import fs from "fs";
inquirer.prompt([
{
name: 'URL',
message: 'What would you like to create a QR to?',
}])
.then(function(answer){
const url=answer.URL;
var qr_svg = qr.image(url);
qr_svg.pipe(fs.createWriteStream('qr.png'));
fs.writeFile("URL.txt",url,(err) => {
if (err) throw err;
console.log("The file has been saved!");
});
});
We will be using three packages - inquirer, qr-image and fs
First we use the inquirer to create a prompt to take a user input (The URL to create the QR code for).
You then extract the URL from the input and create a qr image with it and save this image into the file system.
We then take the user input and save it as a text file.
you can then scan the qr to do all your dirty shenanigans.
Conclusion
So today we learnt what node JS is and how to use it.
Any and all advice is accepted i really do want to learn more : )
github -> https://github.com/P-M-Manmohan
LinkedIn -> https://www.linkedin.com/in/manmohan-p-m-78618323a/