Section 1

Preview this deck

Create a simple http server that writes a message to the page

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

0

All-time users

0

Favorites

0

Last updated

4 years ago

Date created

Mar 1, 2020

Cards (51)

Section 1

(50 cards)

Create a simple http server that writes a message to the page

Front

var http = require('http'); http.createServer(function(req,res) { __res.writeHead(200); __res.end('This is a message'); }).listen(8000);

Back

Write a http server that reads the contents of a file and includes it in the response

Front

var http = require('http'); var fs = require('fs'); http.createServer(function(req, res){ __fs.readFile('filename.ext', function(err, contents){ ____res.end(contents); __}); }).listen(8000);

Back

Create a get express route with a name variable in the path which is written to the response

Front

app.get('/:name', function(req, res) { __res.end(req.params.name); }).listen(8080);

Back

Write a new EventEmitter object called 'chat' which listens for the 'message' event and logs the message to console. Make the 'chat' object emit a message event.

Front

var EventEmitter = require('events').EventEmitter; var chat = new EventEmitter(); chat.on('message', function(message) { __console.log(message); }); chat.emit('message', 'this is the message');

Back

Write a http server that extracts the path from the request and prints it to the page

Front

var http = require('http'); http.createServer(function(req, res) { __req.end(res.url) }).listen(8080);

Back

Using pipe, make a copy of a file, appending the filename with '_copy'

Front

var fs = require('fs'); var file = fs.createReadStream('readme.md'); var newFile = fs.createWriteStream('readme_copy.md'); file.pipe(newFile);

Back

What is express?

Front

A Sinatra inspired web development framework for Node.js - fast, flexible and simple

Back

Create a http server that dispatches requests to express and is used by socket.io to listen for requests

Front

var app = require('express')(); var server = require('http').createServer(app); var io = require('socket.io')(server);

Back

How would you find modules?

Front

1. npm search request 2. npm registry: https://www.npmjs.com/package/npm-registry 3. github search

Back

Create a http server and set the response header to have content type of 'text/html'.

Front

var http = require('http'); http.createServer(function(req, res) { __response.writeHead(200, { ____'Content-Type': 'text/html' __}) }).listen(8000);

Back

Create a http server which logs a 'shutting down' message on the close event

Front

var http = require('http'); var server = http.createServer(); server.on('close', function() { __console.log('Shutting down'); });

Back

Add code to index.html that will ensure the socket.io library gets loaded by the client, and connect to it on localhost port 8080

Front

<script src="/socket.io/socket.io.js"></script> <script> __var socket = io.connect('http://localhost:8080'); </script>

Back

Use pipe to read a file and print its contents to the console.

Front

var fs = require('fs'); var file = fs.createReadStream('filename.ext'); file.pipe(process.stdout);

Back

In a http server, what does: request.pipe(response); do behind the scenes? (write the long version of the code).

Front

request.on(readable, function(){ __var chunk = null; __while(null !== (chunk = request.read())) { ____response.write(chunk); __} }); request.on('end', function() { __response.end(); });

Back

什麼是 error-first callback ?

Front

error-first callback 用來傳遞錯誤和數據。第一個參數永遠是一個錯誤對象(error-object),回調函數必須檢查它。余下的參數用不過來傳遞數據。 fs.readFile(filePath, function(err, data) { if (err) { //處理出現錯誤的情況 } //處理數據 });

Back

Create an express route that accepts GET requests with a name parameter and renders an EJS file called 'hello.ejs' which displays that parameter.

Front

in app.js: var app = require('express')(); app.get('/:name', function(req, res) { __res.render('hello.ejs', { name: req.params.name }); }).listen(8080); in hello.ejs: <%= name %>

Back

use pipe to copy a file, but keep the Write stream open and dispatch a seperate end event.

Front

var fs = require('fs'); var file = fs.createReadStream('existingfile.md'); var copiedFile = fs.createWriteStream('newfile.md'); file.pipe(copiedFile, { end: false }); file.on('end', function() { __copiedFile.end('it has been done!'); });

Back

Read a file and console log the contents synchronously

Front

var fs = require('fs); console.log(fs.readFileSync('filename.txt'));

Back

Write a server that listens on port 8080 and logs the body of requests to the console, with a message confirming when the request has finished sending data, and end the response.

Front

var http = require('http'); http.createServer(function(req, res) { __req.on('data', function(chunk) { ____console.log(chunk); __}); __req.on('end', function() { ____console.log('No more data in request'); ____res.end(); __}); }).listen(8080);

Back

How to export multiple functions and use in another .js file?

Front

in the module.js file: module.exports = { __functionOne: function() { ... }, __functionTwo: function() { ... } }; in the app.js file: var modules = require('./module'); modules.functionOne(); module.functionTwo();

Back

where does `npm install request` install a module to?

Front

./node_modules is default location. 'npm install request -g' to install globally, which will install to

Back

1. How to specify you want a version of a module greater than 1.8.7 but less than 1.9.0 in your dependencies? 2. How to specify a version between 1.0.0 and 2.0.0? 3. Why is it dangerous to specify a range excluding the patch version?

Front

1. in package.json: "module": "~1.8.7" 2. "module": "~1" 3. Might be minor versions that break things, better to just keep up to date with patches.

Back

什麼是stub?說出它的用途?

Front

Stubs是模擬模塊或組件行為的程序。 Stubs提供已知的答案來調用函數,另外你還可以斷言哪個stubs被調用。 var fs = require('fs'); var readFileStub = sinon.stub(fs, 'readFile', function (path, cb) { return cb(null, 'filecontent'); }); expect(readFileStub).to.be.called; readFileStub.restore(); 這個問題有什麼幫助? 這個問題考察面試者的測試知識,如果他不知道什麼是Stubs,你可以問他是如何進行單元測試的。

Back

如何避免回调地狱

Front

你可以有如下几个方法: 模块化:将回调函数分割为独立的函数 使用Promises 使用yield来计算生成器或Promise 解析:这个问题有很多种答案,取决你使用的场景,例如ES6, ES7,或者一些控制流库。

Back

1. Where woud you define your dependencies for your app? 2. How would you install these dependencies?

Front

1. my_app/package.json in { "dependencies": { "module": "1.0.0" } } 2. npm install

Back

When to use the http module vs the net module?

Front

http is built on top of net (which handles the TCP layer underlying HTTP).

Back

What is the socket.io module used for?

Front

Back

What is a duplex stream?

Front

One that can be both read from and written to

Back

為什麼 npm shrinkwarp 非常有用?

Front

This command locks down the versions of a package』s dependencies so that you can control exactly which versions of each dependency will be used when your package is installed. - npmjs.com 這個命令在部署Node.js應用時是非常有用的——它可以保證所部屬的版本就是依賴的版本。

Back

Using socket.io and express, write a server and client where the client can send a message to the server on a button click, which will then console log the output.

Front

In index.ejs: <script src="/socket.io/socket.io.js/"></script> <script src="jquery source"></script> <script> __var socket = io.connect('http://localhost:8080'); __$(document).ready(function() { ____$("#button1").click(function() { ______socket.emit('messages', { message: 'Have a response' }); ____}); __}); in app.js: var app = require('express')(); var server = require('http').createServer(app); var io = require('socket.io')(server); io.on('connection', function(client) { __client.on('messages', function(data) { ____console.log(data.message); __}); });

Back

What does http.get() take as a parameter and what will it return?

Front

parameters are http.get(url, callback). The callback receives a response object which is a Node Stream object, an object that emits events

Back

1. What npm option would you use to install a module globally, and 2. where would this install it to? 3. Why would you want to install globally?

Front

1. npm install module -g 2. /usr/local/lib/node_modules 3. If you wanted to use the module in the command line. (would still need to npm install module in the working directory to use in app .js file)

Back

How to automatically add a module to dependencies when using npm to install it?

Front

npm install --save modulename

Back

Write a client that sends a POST request to localhost on port 8080 with a string in the body, and log the response to console.

Front

var http = require('http'); var request = http.request(options, function(response) { __response.on('data', function(chunk) { ____console.log(chunk); __}); }); request.write('this is the string'); request.end();

Back

Node程序如何監聽80端口?

Front

腦筋急轉彎!你不應該直接使用Node監聽80端口(在*nix系統中),這樣做需要root權限,對於運行程序來說這不是一個好主意。 不過,你可以使Node監聽1024以上的端口,然後在Node前面部署nginx反向代理。

Back

Create an express route that accepts GET requests on /route and responds by sending back a static html file called file.html and listens on port 8080.

Front

var express = require('express'); var app = express(); app.get('/route', function(req, res) { __res.sendFile(__dirname + '/file.html'); }).listen(8080);

Back

什麼是測試金字塔?在做HTTP API的時候要怎麼實現?

Front

測試金字塔意思是在寫測試時應該編寫的底層但願測試要多於高級的端到端測試。 對於HTTP APIs,應該歸結為: 對你的模型多很多單元測試 在你的模型與其他交互時更少的集成測試 更少的驗收測試,在HTTP端 這個問題有什麼幫助? 這個問題告訴你面試者在測試上有多少經驗,特別是他能在每一個級別給出建議。

Back

Add an event listener on a http serve that listens to the 'request' event. The event listener should take a callback function with two arguments, request and response.

Front

var http = require('http'); var server = http.createServer(); server.on('request', function(request, response){ __do stuff... });

Back

Read a file and console log the contents asynchronously

Front

var fs = require('fs'); fs.readFile('filename.txt', callback); function callback(err, contents){ __console.log(contents); };

Back

Create a server that writes the body of a http request into a text file, using pipe.

Front

var fs = require('fs'); var http = require('http'); http.createSever(function(request, response) { __var newFile = fs.createWriteStream('file.md'); __request.pipe(newFile); __request.on('end', function() { ____response.end('Uploaded!'); __}); }).listen(8080);

Back

In the version number of a module what do each of the three digits represent?

Front

first = major version second = minor version third = patch

Back

How to set a time delay on an event

Front

http.createServer(function(req, res) { __res.write('First msg'); __setTimeout(function(){ ____res.write('Second msg'); ____res.end(); __}, 5000); }).listen(8080);

Back

What is a stream?

Front

A stream is an abstract interface implemented by various objects in Node. For example a request to an HTTP server is a stream, as is stdout. Streams are readable, writable, or both. All streams are instances of EventEmitter

Back

What are websockets?

Front

WebSockets is an advanced technology that makes it possible to open an interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.

Back

If you don't specify a patch when requiring a module, what are the locations it will look in? i.e. var required = require('make_request'); within /home/user/my_app/app.js

Front

1. /home/user/my_app/node_modules/make_request.js 2. /home/user/node_modules/make_request.js 3. /home/node_modules/make_request.js 4. /node_modules/make_request.js

Back

How to export a single function and use it in another .js file?

Front

In the module.js file: module.exports = function () { ... } In the app.js file: var mod = require('./module.js')

Back

what does stream.pipe(...) do? what are its arguments?

Front

Connects this read stream to destination WriteStream. Is a method used to take a readable stream and connect it to a writeable steam Incoming data on this stream gets written to destination. The destination and source streams are kept in sync by pausing and resuming as necessary. Arguments are (destination, [options])

Back

Use the request module to make a request to http://search.twitter.com/search.json?q=codeschool and log the response to the console

Front

var request = require('request'); var url = 'http://search.twitter.com/search.json?q=codeschool' request(url, function(error, response, body) { __console.log(body); });

Back

Tell socket.io to listen for a 'connection' event on a http server and log 'Connected' to the console.

Front

var server = require('http').createServer(); var io = require('socket.io')(server); io.on('connection', function(client0 { __console.log('Connected'); });

Back

Use the url module to construct the following url: http://search.twitter.com/search.json?q=codeschool

Front

var url = require('url'); options = { __protocol: 'http:', __host: 'search.twitter.com', __pathname: 'search.json', __query: { q: 'codeschool' } }; var searchUrl = url.format(options);

Back

Section 2

(1 card)

What

Front

Back