Home >Web Front-end >JS Tutorial >Summary of Node.js interview questions (with answers)
This article brings you a summary of the interview questions about Node.js (with answers). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
##Translator’s note: From ECMAScript standard, Node.js syntax and NPM From a module perspective, the development of Node.js is dizzying, so interview questions must also keep pace with the times.
In order to ensure readability, this article uses free translation rather than literal translation.
Questionfs.readFile(filePath, function(err, data) { if (err) { // 处理错误 return console.log(err); } console.log(data); });2. How to avoid callback hell? You can avoid callback hell in the following ways:
catch is used for error handling. Multiple Promises can be chained.
new Promise((resolve, reject) => { setTimeout(() => { resolve('result'); }, 100) }) .then(console.log) .catch(console.error);4. What tools are used to ensure consistent coding style? Why is this so? When collaborating in a team, it is very important to ensure a consistent coding style so that team members can modify the code faster without having to adapt to a new style every time. These tools can help us:
var fs = require('fs'); var writeFileStub = sinon.stub(fs, 'writeFile', function(path, data, cb) { return cb(null); }); expect(writeFileStub).to.be.called; writeFileStub.restore();6. What is the testing pyramid? For example, the test pyramid reflects the proportion of
unit tests, integration tests and end-to-end tests that need to be written:
When testing the HTTP interface, it should look like this:
set-cookie needs to be configured in the HTTP header:
cross-site scripting, Because it will prevent Javascript scripts from accessing cookies.
Set-Cookie: sid=
new Promise((resolve, reject) => { throw new Error('error') }) .then(console.log)
thenThere is no catch after that. This way, errors will be ignored. You can solve the problem like this:
new Promise((resolve, reject) => { throw new Error('error') }) .then(console.log).catch(console.error)
调试一个大型的项目时,可以使用监控unhandledRejection事件来捕获所有未处理的Promise错误:
process.on('unhandledRejection', (err) => { console.log(err) })2. 这段代码有什么问题?
function checkApiKey(apiKeyFromDb, apiKeyReceived) { if (apiKeyFromDb === apiKeyReceived) { return true } return false }
比较密码时,不能泄露任何信息,因此比较必须在固定时间完成。否则,可以使用timing attacks来攻击你的应用。为什么会这样呢?Node.js使用V8引擎,它会从性能角度优化代码。它会逐个比较字符串的字母,一旦发现不匹配时就停止比较。当攻击者的密码更准确时,比较的时间越长。因此,攻击者可以通过比较的时间长短来判断密码的正确性。使用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,逐行解释如下:
创建新的Promise,resolve值为1。
x为1,加1之后返回2。
x为2,但是没有用到。抛出一个错误。
捕获错误,但是没有处理。返回1。
x为1,加1之后返回2。
x为2,打印2。
不会执行,因为没有错误抛出。
The above is the detailed content of Summary of Node.js interview questions (with answers). For more information, please follow other related articles on the PHP Chinese website!