[译]Node.js Interview Questions and Answers (2017 Edition)
Node.js Interview Questions for 2017
- 什么是error-first callback?
- 如何避免无止境的callback?
- 什么是Promises?
- 用什么工具来保证代码的一致性风格? 为什么保持一致性风格很重要?
- When should you npm and when yarn?
- 什么是stub? 举个例子!
- 什么是test pyramid? 举个例子!
- 你喜欢哪个HTTP框架,为什么?
- 如何保护你的HTTP cookies 不遭受XSS攻击?
- 如何确保你的依赖是安全的?
什么是error-first callback
Error First callback用来传递error和data。 使用error作为第一个参数,要检查这个error参数看看是不是哪出错了。第二个参数用来传递data。
fs.readFile(filePath, function(err, data) {
if (err) {
// handle the error, the return is important here
// so execution stops here
return console.log(err)
}
// use the data object
console.log(data)
})
如何避免无止境的callback
有许多方法:
- modularization: break callbacks into independent functions
- 使用流控制库, 例如async
- 使用generators with Promises 详见
- 使用async/await(这个是v7才有的, 你可以看看我们关于async/await的实践)
什么是Promises
Promises帮助我们更好的处理异步操作。
下面的例子中100ms后打印一个result字符串。 注意到了catch没, 它用来处理错误。Promises是可链接式的。
new Promise((resolve, reject) => {
setTimeout(() => {
resolve('result')
}, 100)
})
.then(console.log)
.catch(console.error)
用什么工具来保证代码的一致性风格? 为什么保持一致性风格很重要
在一个团队中工作的时候,一致性风格尤为重要,这样团队成员可以更加简单的修改项目,不要每次都是用一个新的风格。
同时,借助于静态分析可以帮助消除issue。
可以使用以下工具:
什么是stub? 举个例子!
stub用来模拟函数的行为。
var fs = require('fs')
var writeFileStub = sinon.stub(fs, 'writeFile', function (path, data, cb) {
return cb(null)
})
expect(writeFileStub).to.be.called
writeFileStub.restore()
什么是test pyramid? 举个例子
test pyramind描述所需单元测试,集成测试,End-to-End的比率。

你喜欢哪个HTTP框架,为什么
没有正确答案。目的是考察对某个框架的了解程度。
background/worker在什么时候有用? 你是如何处理worker task的
worker processes对于在后台进行数据处理非常有用,例如发送邮件或者处理图片。
如何保护你的HTTP cookies 不遭受XSS攻击
XSS是攻击者将可执行的js注入到你的系统中。
减少这种攻击,你应该设置HTTP请求头set-cookie:
- HttpOnly 用来阻止通过js来方位cookie
- secure 告诉浏览器只用在https的情况下才发送cookie
如果你用的是Express, 使用express-cookie session。
如何确保你的依赖是安全的
nodejs项目很容易就依赖上百上千个依赖包。 手工检查是不现实的。
我们可以自动更新、安全审查依赖。下面是一些免费和付费的方案:
npm outdated- Trace by RisingStack
- NSP
- GreenKeeper
- Snyk
面试题
下面的代码有什么问题?
new Promise((resolve, reject) => {
throw new Error('error')
}).then(console.log)
解决方案
then后面没有catch。
改为
new Promise((resolve, reject) => {
throw new Error('error')
}).then(console.log).catch(console.error)
如果你在debug一个大项目,你不知道哪个Promise可能会有这个issue, 你可以使用unhandleRejection钩子。它会打印所用unhandled Promise。
process.on('unhandledRejection', (err) => {
console.log(err)
})
下面的代码有什么问题?
function checkApiKey (apiKeyFromDb, apiKeyReceived) {
if (apiKeyFromDb === apiKeyReceived) {
return true
}
return false
}
解决方案
比较安全凭证的关键是不泄露任何信息,你应该确保你在一个固定的时间段比较他们。 如果你不这么做你很容易遭受时序攻击(timing attacks)。
时序攻击属于侧信道攻击/旁路攻击(Side Channel Attack),侧信道攻击是指利用信道外的信息,比如加解密的速度/加解密时芯片引脚的电压/密文传输的流量和途径等进行攻击的方式,一个词形容就是“旁敲侧击”。
举一个最简单的计时攻击的例子,某个函数负责比较用户输入的密码和存放在系统内密码是否相同,如果该函数是从第一位开始比较,发现不同就立即返回,那么通过计算返回的速度就知道了大概是哪一位开始不同的,这样就实现了电影中经常出现的按位破解密码的场景。密码破解复杂度成千上万倍甚至百万千万倍的下降。
最简单的防御方法是:“发现错误的时候并不立即返回,而是设一个标志位,直到完全比较完两个字符串再返回”。
时序攻击并非是一种理论攻击方法,OpenSSL、OpenSSH等应用都曾经有时序攻击漏洞,举个实际的例子吧:
作者:shotgun
链接:https://www.zhihu.com/question/20156213/answer/43377769
来源:知乎
你可以使用cryptiles来解决这个问题。
function checkApiKey (apiKeyFromDb, apiKeyReceived) {
return cryptiles.fixedTimeComparison(apiKeyFromDb, apiKeyReceived)
}
下面的代码将输出什么
Promise.resolve(1)
.then((x) => x + 1)
.then((x) => { throw new Error('My Error') })
.catch(() => 1)
.then((x) => x + 1)
.then((x) => console.log(x))
.catch(console.error)
输出2,看看为什么输出2:
- 新的Promise创建, resolve 1
- resolve加1,变成2
- 抛出一个异常
- 返回1
- 加1,变成2
- 打印出2
- 这行不会执行,因为没有异常
[译]Node.js Interview Questions and Answers (2017 Edition)的更多相关文章
- 101+ Manual and Automation Software Testing Interview Questions and Answers
101+ Manual and Automation Software Testing Interview Questions and Answers http://www.softwaretesti ...
- Top 25 Most Frequently Asked Interview Core Java Interview Questions And Answers
We are sharing 25 java interview questions , these questions are frequently asked by the recruiters. ...
- [译]Node.js - Event Loop
介绍 在读这篇博客之前,我强列建议先阅读我的前两篇文章: Getting Started With Node.js Node.js - Modules 在这篇文章中,我们将学习 Node.js 中的事 ...
- 115 Java Interview Questions and Answers – The ULTIMATE List--reference
In this tutorial we will discuss about different types of questions that can be used in a Java inter ...
- 69 Spring Interview Questions and Answers – The ULTIMATE List--reference
This is a summary of some of the most important questions concerning the Spring Framework, that you ...
- 译\Node.js应用的持续部署
Node.js应用的持续部署 翻译前 翻译自:https://blog.risingstack.com/continuous-deployment-of-node-js-applications/ 正 ...
- [译]Node.js : Building RESTful APIs using Loopback and MySQL
国庆后可能就要使用StrongLoop那套东西来做项目了 原文:http://www.javabeat.net/loopback-mysql/ Loopback是什么? Loopback是一个开源的N ...
- [译]Node.js Best Practices - Part 2
原文: https://blog.risingstack.com/node-js-best-practices-part-2/ 统一风格 在大团队开发JS应用, 创建一个风格指南是很有必要的. 推荐看 ...
- [译]Node.js面试问与答
原文: http://blog.risingstack.com/node-js-interview-questions/ 什么是error-first callback? 如何避免无休止的callba ...
随机推荐
- linux 做gw(nat)详细配置
linux 做企业网关gw(nat)详细配置 最近因为公司的路由器老化导致上网时断时续,上半小时网就断一次网,为此我头疼不已,本着为公司节约成本的宗旨, ...
- auto_clipboard
黄山松发表于博客园:http://www.cnblogs.com/tomview/p/6137179.html #ifndef __HSS_AUTO_CLIPBOARD_HSS__#define __ ...
- Mellanox 8亿美元收购EZchip
http://www.iccsz.com/site/cn/News/2015/10/08/20151008003916131771.htm Mellanox 8亿美元收购EZchip 目标电信运营商 ...
- Windows 7 在资源管理器中显示软件快捷方式
该方法是利用资源管理器中储存网络位置的文件夹实现的, 不需要修改注册表. 效果如图: 操作方法: 在资源管理器中打开路径 "%appdata%\Microsoft\Windows\Netwo ...
- webform Repeater重复器、地址栏传值、Response
Repeater: 重复器 <HeaderTemplate></HeaderTemplate> - 头模板:在循环开始时,其内容只会打印一遍 <ItemTemplate& ...
- 数组为什么可以使用linq查询
问题引出 这视乎是个完全不必要进行讨论的话题,因为linq(这里具体是linq to objects)本来就是针对集合类型的,数组类型作为集合类型的一种当然可以使用了.不过我还是想写一下,这个问题源于 ...
- const 引起的BUG
今天白天出现了碰见了一个问题,隐藏得比较深,这里记录一下. 初衷很简单,就是要更改改一个数据库的链接名,这个链接名是放在数据层public const string connDB="conn ...
- Hihocoder 太阁最新面经算法竞赛18
Hihocoder 太阁最新面经算法竞赛18 source: https://hihocoder.com/contest/hihointerview27/problems 题目1 : Big Plus ...
- 51Nod 1428 活动安排问题
51Nod 1428 活动安排问题 Link: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1428 1428 活 ...
- caffe代码调试小结
RELULayer层 bottom[0]->count=n*c*w*h=50*96*56*56 count=50*96*56*56,根据bottom_data[i]访问所有的数据(多维数组都是一 ...