Y combinator - Paul Graham
Full stack = Front end + Back end
Server is computer that connected to networks Application is Logic Respond to request Database is used to store the data - Persist the information
Why we need framework? Framework is pre-built componet and create a finish products to build an application without writing code from scratch.
What is Node.js? As an asynchronous event-driven JavaScript runtime, Node.js is designed to build scalable network applications. https://nodejs.org/en/about
Javascript runs on V8 engine https://en.wikipedia.org/wiki/V8_(JavaScript_engine)
Node Read Eval Print Loop (Node REPL) is a computer environment where user inputs are read and evaluated, and then the results are returned to the user.
In terminal, just type node
and hit enter.
$ node
You will see the welcome note(you are in REPL environment)and right hand arrow >
and type .help
, to know about REPL.
ctrl
+ c
to exit the REPL environment
create a index.js
file in any folder like (Node folder), write a code console.log("Hello world!")
and then open terminal type node index.js
. You will see the output as Hello world!
.
Native node modules? It's a pre-built components to work on node js environment.
Here we are using CJS - Common JavaScript, ESM(ECMAScript Module) we will next lesson.
https://nodejs.org/dist/latest-v18.x/docs/api/fs.html fs means File system.
const fs = require("fs");
fs.writeFile("message.txt", "Hello World!", (err) => {
if (err) throw err;
console.log('The file has been saved!');
});
const fs = require("fs");
fs.readFile("message.txt", 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
});
To initialize npm we have to use npm init
in terminal
$ npm init
Then it shows on screen a utility will walk you through creating a package.json file. Type the information based on it ask.
{
"name": "hello-world",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Salman",
"license": "ISC"
}
Search in NPM - Node Package Manager website as sillyname
. A random name generator.
$ npm install <something>
we can write i
instead of install
, it's an short-hand command.
$ npm i sillyname
From website copy the two lines of code and paste into index.js
var generateName = require('sillyname');
var sillyName = generateName();
console.log(`My name is ${sillyName}.`);
- Run index.js in terminal
node index.js
To use ESM, we have to add dependencies in package.json
file as "type":"module",
.
{
"name": "hello-world",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type":"module", // <<=======
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Salman",
"license": "ISC"
}
ESM
is new module. Default type is CJS
- CJS
var pack = require('<packageName>');
var pack = require('sillyname');
- ESM
import pack from "<packageName>";
import pack from "sillyname";
// var generateName = require('sillyname');
import generateName from "sillyname";
var sillyName = generateName();
console.log(`My name is ${sillyName}.`);
npm package superheroes creator inspiration
- Use the inquirer npm package to get user input.
- Use the qr-image npm package to turn the user entered URL into a QR code image.
- Create a txt file to save the user input using the native fs node module.
- install
inquirer
andqr-image
npm and typenpm init
- see the this below code
- run the index.js by typing
node index.js
/*
1. Use the inquirer npm package to get user input.
2. Use the qr-image npm package to turn the user entered URL into a QR code image.
3. Create a txt file to save the user input using the native fs node module.
*/
import inquirer from "inquirer";
import qr from "qr-image";
import fs from "fs";
inquirer
.prompt([
{
message: "Type in your URL: ",
name: "URL",
},
])
.then((answers) => {
// Use user feedback for... whatever!!
const input = answers.url;
var qr_svg = qr.image(input, { type: "svg" });
qr_svg.pipe(fs.createWriteStream("image-qr.svg"));
fs.writeFile("URL.txt", url, (err) => {
if (err) throw err;
console.log("The file has been saved!");
});
})
.catch((error) => {
if (error.isTtyError) {
// Prompt couldn't be rendered in the current environment
console.log("Erro: cannot be rendered");
} else {
// Something else went wrong
console.log("Error undefined");
}
});