原文

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

有许多方法:

什么是Promises

Promises帮助我们更好的处理异步操作。

下面的例子中100ms后打印一个result字符串。 注意到了catch没, 它用来处理错误。Promises是可链接式的。

new Promise((resolve, reject) => {
setTimeout(() => {
resolve('result')
}, 100)
})
.then(console.log)
.catch(console.error)

用什么工具来保证代码的一致性风格? 为什么保持一致性风格很重要

在一个团队中工作的时候,一致性风格尤为重要,这样团队成员可以更加简单的修改项目,不要每次都是用一个新的风格。

同时,借助于静态分析可以帮助消除issue。

可以使用以下工具:

推荐阅读Javascript Clean Coding

什么是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对于在后台进行数据处理非常有用,例如发送邮件或者处理图片。

可以使用RabbitMQKafka

如何保护你的HTTP cookies 不遭受XSS攻击

XSS是攻击者将可执行的js注入到你的系统中。

减少这种攻击,你应该设置HTTP请求头set-cookie:

  • HttpOnly 用来阻止通过js来方位cookie
  • secure 告诉浏览器只用在https的情况下才发送cookie

如果你用的是Express, 使用express-cookie session

如何确保你的依赖是安全的

nodejs项目很容易就依赖上百上千个依赖包。 手工检查是不现实的。

我们可以自动更新、安全审查依赖。下面是一些免费和付费的方案:

面试题

下面的代码有什么问题?

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:

  1. 新的Promise创建, resolve 1
  2. resolve加1,变成2
  3. 抛出一个异常
  4. 返回1
  5. 加1,变成2
  6. 打印出2
  7. 这行不会执行,因为没有异常

[译]Node.js Interview Questions and Answers (2017 Edition)的更多相关文章

  1. 101+ Manual and Automation Software Testing Interview Questions and Answers

    101+ Manual and Automation Software Testing Interview Questions and Answers http://www.softwaretesti ...

  2. 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. ...

  3. [译]Node.js - Event Loop

    介绍 在读这篇博客之前,我强列建议先阅读我的前两篇文章: Getting Started With Node.js Node.js - Modules 在这篇文章中,我们将学习 Node.js 中的事 ...

  4. 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 ...

  5. 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 ...

  6. 译\Node.js应用的持续部署

    Node.js应用的持续部署 翻译前 翻译自:https://blog.risingstack.com/continuous-deployment-of-node-js-applications/ 正 ...

  7. [译]Node.js : Building RESTful APIs using Loopback and MySQL

    国庆后可能就要使用StrongLoop那套东西来做项目了 原文:http://www.javabeat.net/loopback-mysql/ Loopback是什么? Loopback是一个开源的N ...

  8. [译]Node.js Best Practices - Part 2

    原文: https://blog.risingstack.com/node-js-best-practices-part-2/ 统一风格 在大团队开发JS应用, 创建一个风格指南是很有必要的. 推荐看 ...

  9. [译]Node.js面试问与答

    原文: http://blog.risingstack.com/node-js-interview-questions/ 什么是error-first callback? 如何避免无休止的callba ...

随机推荐

  1. Merge 的小技巧

    今天跟大家分享一下搬动数据使用Merge的方法. 有些时候,当我们做数据搬动的时候,有时候做测试啊,换对象啊,就会存在有时候外键存在,不知道怎么对应的关系.比如我现在有架构相同的两组table , A ...

  2. Block入门

    iOS4.0开始,Block横空出世,它其实就是c预言的补充,书面点说就是带有自动变量的匿名函数,Block简洁,代码的可读性也高,因此深受广大开发者的喜爱,这一次给大家介绍Block的基本类型和项目 ...

  3. 学习 shell脚本之前的基础知识

    转载自:http://www.92csz.com/study/linux/12.htm  学习 shell脚本之前的基础知识 日常的linux系统管理工作中必不可少的就是shell脚本,如果不会写sh ...

  4. 【原】移动web滑屏框架分享

    本月26号参加webrebuild深圳站,会上听了彪叔的对初心的讲解,“工匠精神”这个词又一次被提出,也再次引起了我对它的思考.专注一个项目并把它做得好,很好,更好...现实工作中,忙忙碌碌,抱着完成 ...

  5. Unity中关于作用力方式ForceMode的功能注解

    功能注解:ForceMode为枚举类型,用来控制力的作用方式,有4个枚举成员,在以下举例中均设刚体质量为m=2.0f,力向量为f=(10.0f,0.0f,0.0f). (1)ForceMode.For ...

  6. 检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005

    检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005 在CSDN上总是有网友问这个 ...

  7. phabricator-zh_CN汉化包

    https://github.com/wanthings/phabricator-zh_CN 之前公司用这个做任务管理.可以将一些安排计划等指派给某个人跟进进度. 这个系统都是英文的,这是个缺憾.我找 ...

  8. [LeetCode] Valid Anagram 验证变位词

    Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = & ...

  9. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  10. BootStrap table使用

    bootstrap table git address https://github.com/wenzhixin/bootstrap-table 引入文件 <link rel="sty ...