1 of 34

NodeJS

Step by Step

2 of 34

How do you start a program on windows or mac?

3 of 34

Click visual icons

4 of 34

How do you start a nodejs app?

5 of 34

Node is only accessible through the terminal

6 of 34

If you just type node, it takes over the terminal and turns it into a javascript machine

7 of 34

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.

8 of 34

What do we want our node javascript code to do?

9 of 34

Node has no UI :)

There's no textboxes. How do we get input?

10 of 34

Process arguments

Npm is the program we want to run

But what is init ?

11 of 34

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

12 of 34

One tool is capable of doing many things.

How is it supposed to know what you want it to do?

13 of 34

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");

}

14 of 34

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();

});

15 of 34

Where did readline come from?

const readline = require('readline');

16 of 34

You have to be aware of what is built into nodejs.

readline just exists because it was coded into nodejs directly

17 of 34

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

18 of 34

19 of 34

New vscode specific file is created

Make sure this config is chosen in the drop down before you click play

20 of 34

Where does output go?

21 of 34

We can not make attractive user interfaces as we can with html and web pages

https://nodejs.org/api/console.html

22 of 34

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();

});

23 of 34

This seems silly. Nodejs can’t do much, why are we using it?

24 of 34

Just this limited functionality is enough to build a simple game

25 of 34

Playing a game in the console is not really done these days. ...

So what do people want to do?

26 of 34

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.

27 of 34

I want to build something, I only know javascript. Do I want to build something or learn another programming language.

28 of 34

The most useful thing nodejs can do is read and write files.

Create folder node-practice

Create file index.js

29 of 34

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!");

});

30 of 34

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);

});

31 of 34

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

32 of 34

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);

});

33 of 34

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);

34 of 34

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);