Node Flashcards

1
Q

http.createServer - what to require

A

var http = require(‘http’);

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

http.createServer - hello world

A

http.createServer(function(req,res){
res.writeHead(200,{‘Content-Type’: ‘text/html’});
res.end(‘Hello World’);
}).listen(8080);

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

use the “exports” keyword to make properties and methods available outside the module

A
exports.myDateTime = function() {
return Date();
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

include a module

A

var dt = require(‘./myfirstmodule’);

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

what does the built-in Node module ‘http’ do

A

allows to transfer data over the HTTP protocol

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

If the response from the HTTP server is supposed to be displayed as HTML…

A

include HTTP header with the correct content type

res.writeHead(200, {‘Content-Type’: ‘text/html’})

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

the function passed into http.createServer has req argument - it represents…

A

the request from the client, as an object (http.IncomingMessage)

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

the built-in URL module can easily _ query string

A

split into readable parts

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

URL can have a query in it, using URL module, like this

A
var q = url.parse(req.url, true).query //will pick apart the url query
var txt = q.year + " " + q.month //will render in text

in the URL, put /?year=2017&month=July and it will use these values in txt

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

file system (‘fs’) allows to work with the file system. use for …

A

reading, creating, updating, renaming and deleting files

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

read files with

A

fs.readFile()

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

how to read in an HTML file

A
inside http.createServer you place another function with fs.readFile('index.html',function(err,data){
//res.writeHead...
//res.write...
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

FS module has these methods for creating files

A

fs. appendFile()
fs. open()
fs. writeFile()

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

fs.appendFile() does…

A

appends specified content to a file

if the file doesn’t exist, it creates it

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

fs.open() takes a flag as second argument

A

if the flag is ‘w’ for writing, the specified file is open for writing

if the file doesn’t exist, an empty one is created

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

fs.writeFile()

A

replaces the specified file and content if it exists

if the file doesn’t exist, a new file with the new specified content will be created

17
Q

to delete a file with FS…

A

use the fs.unlink() method

18
Q

rename files with

A

fs.rename(old name, new name)

19
Q

url.parse() does…

A

parses an address, returns a URL object with each part of the address as properties

url.parse(address, true)

20
Q

The NPM program is installed on your computer

A

when you install Node.js

21
Q

A package in Node.js contains

A

all the files you need for a module.

22
Q

Modules are JavaScript

A

libraries you can include in your project.

23
Q

NPM creates a folder named

A

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

24
Q

Include the newly installed package

A
the same way you include any other module:
var uc = require('upper-case');
25
Node.js is perfect for ___ applications.
event-driven
26
Objects in Node.js can fire
events, like the readStream object fires events when opening and closing a file
27
Node.js has a built-in module, called "Events",
where you can create-, fire-, and listen for- your own events. include with require('events')
28
all event properties and methods are an instance of an
EventEmitter object
29
To be able to access these properties and methods, create an EventEmitter object:
``` var events = require('events'); var eventEmitter = new events.EventEmitter(); ```
30
with the EventEmitter object You can assign
event handlers to your own events
31
To fire an event
use the emit() method.
32
a very good module for working with file uploads
Formidable
33
When the file is uploaded and parsed, it gets placed..
on a temporary folder on your computer
34
The path to this temp directory can be found in the
"files" object, passed as the third argument in the parse() method's callback function
35
send emails from your computer
Nodemailer module