NodeJS
Step by Step
How do you start a program on windows or mac?
Click visual icons
How do you start a nodejs app?
Node is only accessible through the terminal
If you just type node, it takes over the terminal and turns it into a javascript machine
This is fine for testing but not very useful as all our code is in files.
Type node filename.js to have node run your file.
What do we want our node javascript code to do?
Node has no UI :)
There's no textboxes. How do we get input?
Process arguments
Npm is the program we want to run
But what is init ?
NPM itself can do many things.
It would have been silly if they built a separate tool for every separate thing npm can do
Npm-install
Npm-publish
npm- init
Npm-adduser
One tool is capable of doing many things.
How is it supposed to know what you want it to do?
console.log(process.argv);
process.argv.forEach(function (val, index, array) {
console.log(index + ': ' + val);
});
if(process.argv[2] === "init"){
//do init stuff� console.log("I am doing init stuff");
}
if(process.argv[2] === "install"){
console.log("I am doing install stuff");
}
if(process.argv[2] === "public"){
console.log("I am doing public stuff");
}
Node takes user input from the terminal
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('What do you think of Node.js? ', (answer) => {
console.log(`Thank you for your valuable feedback: ${answer}`);
rl.close();
});
Where did readline come from?
const readline = require('readline');
You have to be aware of what is built into nodejs.
readline just exists because it was coded into nodejs directly
Debugging
If you start your program by debugging where do the arguments get put in?
Where does the readline input get typed in?
This requires some additional setup for the debugger
New vscode specific file is created
Make sure this config is chosen in the drop down before you click play
Where does output go?
We can not make attractive user interfaces as we can with html and web pages
https://nodejs.org/api/console.html
You must console.log everything
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('What do you think of Node.js? ', (answer) => {
console.log(`Thank you for your valuable feedback: ${answer}`);
rl.close();
});
This seems silly. Nodejs can’t do much, why are we using it?
Just this limited functionality is enough to build a simple game
Playing a game in the console is not really done these days. ...
So what do people want to do?
Nodejs is useful for building utility software that doesn’t need a UI
We need to be aware of a few of the things nodejs can do that a browser cannot.
I want to build something, I only know javascript. Do I want to build something or learn another programming language.
The most useful thing nodejs can do is read and write files.
Create folder node-practice
Create file index.js
The most useful thing nodejs can do is read and write files.
Create a file…..�https://nodejs.org/api/fs.html
var fs = require('fs');
fs.writeFile("mynewfile.txt", "Important Info", (err) => {
if (err) throw err;
console.log("The file was succesfully saved!");
});
The most useful thing nodejs can do is read and write files.
Read a file...
const fs = require('fs')
fs.readFile(mynewfile.txt, 'utf8', function (err,data) {
if (err) {return console.log(err);}
console.log(data);
});
Complexity
Sometimes the way node js does things is a little too complicated
It is a common practice for the community to build packages to hide how complicated it is and make it more simple.
Making calls to APIs is one of those things
https://www.twilio.com/blog/2017/08/http-requests-in-node-js.html
Node can send messages to other computers
We used fetch in the browser, so let’s use fetch on the server.
Npm install --save node-fetch
https://www.npmjs.com/package/node-fetch
const fetch = require('node-fetch');
fetch('https://api.github.com/users/github')
.then(res => res.json())
.then(json => {
console.log(json);
});
Node can also listen for messages from other computers
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Hello World!');
res.end();
}).listen(8080);
Node can also listen for messages from other computers
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Hello World!');
res.end();
}).listen(8080);