https://vh7.uk/supper
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js' package ecosystem, npm, is the largest ecosystem of open source libraries in the world.
Print to Console "Fetching Info from Database"
Get Info from Database and Print to Console
Print to Console "Fetching Info from Website"
Get Info from Website and Print to Console
Node.js
Python
Fetching Info from Database
Fetching Info from Website
Website: Jeff
Database: Hello
Fetching Info from Database
Database: Hello
Fetching Info from Website
Website: Jeff
Node.js
Python
. . .
db.query("SELECT * FROM people", def(names):
for name in names:
print(name)
)
. . .
db.query("SELECT * FROM people")
names = db.output;
for name in names:
print(name)
The code on the left is not JavaScript! It is to show how callbacks work in JavaScript with Python code (which won't work in real life!).
NPM stands for Node Package Manager. It's the number one place to go to download Node packages (or libraries or modules).
Like pip for Python but better.
Go to https://runkit.com/ and you should get a 'notebook':
run your code
write your code here
add a new code block (like a new file)
import library from npm
Statements are separated by semicolons:
Comments:
var a = 5;
var b = 10;
var c;
c = a + b;
// Hello There! I'm a comment!
// Print Normal Text to the Console
console.log("Hello World");
function myFunction() {
console.log("Hello!");
}
myFunction();
thisFunctionIsAfter();
function thisFunctionIsAfter() {
console.log(`In Python, functions that are called before
they are defined won't work. Will it in Node.js?`);
}
parrot("hello");
function parrot(text) {
console.log(text);
}
var name = require("library");
var readline = require("readline");
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("What do you think of Node.js? ", function(answer) {
console.log("Thank you for your valuable feedback: ${answer}");
//make sure to close when you've finished
rl.close();
});
// . . .
rl.question("Q1: What is your favourite colour? ", function(answer) {
console.log("Awesome!");
rl.question("Q2: What is your favourite ice cream flavour? ", function(answer) {
console.log("Awesome!");
});
});
// . . .
rl.question("Q1: What is your favourite colour? ", function(answer) {
console.log("Awesome!");
});
rl.question("Q2: What is your favourite ice cream flavour? ", function(answer) {
console.log("Awesome!");
});
var name = "Jeff";
console.log("Hello " + name);
console.log("Hello ${name}");
var age = 18;
console.log("Hello " + name + ". You are " + age + ".");
console.log("Hello ${name}. You are ${age}.");
You'd be right there! Both use SQL queries and they are both databases. MySQL databases are hosted on servers as opposed to inside a file. And the commands for MySQL are a little different to SQLite.
Import with:
var mysql = require("mysql");
Make a new connection:
var connection = mysql.createConnection({
host: "carbon.vh7.uk",
user: "codeclub",
password: "icanhascheezburger",
database: "codeclub"
});
Connect:
Disconnect:
connection.connect();
connection.end();
Execute Queries
connection.query("SELECT * FROM people", function (error, results, fields) {
if (error) throw error;
results.forEach(function(result) {
console.log(result.firstname);
});
});
Ask for help or Google what you want to do followed my MySQL (e.g "insert data mysql").