如果在节点上开发的网站上发生突发请求,该怎么办?

我正在使用Express来构build一个预计会有大量请求的站点(在单个实例中大约有900个)。 我应该怎么做,以避免网站崩溃和最低硬件规格相同。

其他日子我做了一些testing。 没什么奇特的,只是为了玩一下。 如果你做了很多的I / O,非常繁重的计算等,你应该重新考虑一些方面。 上面的例子在Express中的工作原理是一样的…您可以在注释代码之间切换并亲自查看事情的工作原理

// Linux command to generate the file // dd if=/dev/zero of=myFile bs=1300000000 count=1 const http = require('http'); const fs = require('fs'); const port = 34242; server = http.createServer((req, res) => { // // Heady I/O // fs.readFile(__dirname + '/myFile', 'utf8', (err, data) => { // res.end('working hot'); // }); // No I/O res.end('working hot'); }) server.listen(port, () => { console.log(`server started and listening on ${port}`); let count = 0 // // Concomitant request // if No I/O on the server -> fails if reqNumber is more than 400, Error: connect ECONNRESET 127.0.0.1:34242 // if Heavy I/O on the server -> fails, Error: connect ECONNRESET 127.0.0.1:34242 // const reqNumber = 300; // console.time('timeSpent'); // for (let i = 0; i < reqNumber; i++) { // http.get(`http://localhost:${port}`, (res) => { // count++; // if (count == reqNumber) { // console.timeEnd('timeSpent'); // } // }) // } // Small interval between requests // if No I/O on the server -> it works well // if Heavy I/O on the server -> fails Error: connect ECONNRESET 127.0.0.1:34242 let myInterval = setInterval(() => { http.get(`http://localhost:${port}`, (res) => { console.log(count++) }) }, 1) setTimeout(() => { clearInterval(myInterval); }, 10500) })