Node.js Flashcards

1
Q

Node.js as a Web Server

A

The HTTP module can create an HTTP server that listens to server ports and gives a response back to the client.

Use the createServer() method to create an HTTP server:
var http = require('http');

//create a server object:
http.createServer(function (req, res) {
res.write(‘Hello World!’); //write a response to the client
res.end(); //end the response
}).listen(8080); //the server object listens on port 8080
The function passed into the http.createServer() method, will be executed when someone tries to access the computer on port 8080.

Save the code above in a file called “demo_http.js”, and initiate the file:
Initiate demo_http.js:

C:\Users\Your Name>node demo_http.js

If the response from the HTTP server is supposed to be displayed as HTML, you should include an HTTP header with the correct content type:
res.writeHead(200, {‘Content-Type’: ‘text/html’});
The first argument of the res.writeHead() method is the status code, 200 means that all is OK, the second argument is an object containing the response headers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Node.js change detection

A

we need to close and rerun c:> node filename.js for change detection

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Read the Query String

A
var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.write(req.url);
  res.end();
}).listen(8080);

http: //localhost:8080/summer
output: summer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Node.js as a File Server

A

Node.js File System Module
demofile1.html

<h1>My Header</h1>

<p>My paragraph.</p>

---
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
  fs.readFile('demofile1.html', function(err, data) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data);
    res.end();
  });
}).listen(8080);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Node.js URL Module

A
var url = require('url');
var adr = 'http://localhost:8080/default.htm?year=2017&amp;month=february';
var q = url.parse(adr, true);

console. log(q.host); //returns ‘localhost:8080’
console. log(q.pathname); //returns ‘/default.htm’
console. log(q.search); //returns ‘?year=2017&month=february’

var qdata = q.query; //returns an object: { year: 2017, month: 'february' }
console.log(qdata.month); //returns 'february'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

url module with file server

A
var http = require('http');
var url = require('url');
var fs = require('fs');
http.createServer(function (req, res) {
  var q = url.parse(req.url, true);
  var filename = "." + q.pathname;
  fs.readFile(filename, function(err, data) {
    if (err) {
      res.writeHead(404, {'Content-Type': 'text/html'});
      return res.end("404 Not Found");
    } 
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data);
    return res.end();
  });
}).listen(8080);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Node.js NPM

A

NPM is a package manager for Node.js packages, or modules if you like.

www.npmjs.com hosts thousands of free packages to download and use.

The NPM program is installed on your computer when you install Node.js

NPM is already ready to run on your computer!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a Package?

A

A package in Node.js contains all the files you need for a module.

Modules are JavaScript libraries you can include in your project.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Download NPM package

A

Downloading a package is very easy.

Open the command line interface and tell NPM to download the package you want.

I want to download a package called “upper-case”:

Download “upper-case”:

C:\Users\Your Name>npm install upper-case
Now you have downloaded and installed your first package!

NPM creates a folder named “node_modules”, where the package will be placed. All packages you install in the future will be placed in this folder.

My project now has a folder structure like this:

C:\Users\My Name\node_modules\upper-case

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Modules

A

Modules are JavaScript libraries you can include in your project.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

using NPM package

A

Once the package is installed, it is ready to use.

Include the “upper-case” package the same way you include any other module:

var uc = require('upper-case');
Create a Node.js file that will convert the output "Hello World!" into upper-case letters:
Example
var http = require('http');
var uc = require('upper-case');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.write(uc.upperCase("Hello World!"));
  res.end();
}).listen(8080);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly