Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

如何解决访问稍微一多 内存就暴涨!!

$
0
0

1.jpg平常内存是这样的。

然后测试每一秒 100个人请求首页

const request = require('request');
setInterval(() => {
    for (let i = 0; i < 100; i++) {
        request('http://我的网址首页.com', (err, res, body) => {
            if (!err && res.statusCode == 200) {
                console.log(1)
            } else {
                console.log(‘n’)
            }
        })
    }

}, 1000)

然后变这样了。。 2.jpg

/*首页路由如下。。*/
route.get('/', (req, res) => {
    let pageInfo = req.query
    let searchW = { category: 'article' }
    let seo_title = pageInfo.search ? pageInfo.search + "_" : '';
    let seo_tag = pageInfo.tag ? pageInfo.tag + "_" : '';
    pageInfo.page = pageInfo.page && parseInt(pageInfo.page) >= 1 && parseInt(pageInfo.page) <= 200 ? pageInfo.page : 1
    let seo_page = pageInfo.page > 1 ? `第${pageInfo.page}页_` : '';
    let shownum = pageInfo.shownum && pageInfo.shownum < 50 ? parseInt(pageInfo.shownum) : 30;
    if (pageInfo.search) {
        searchW.title = new RegExp(`.*${pageInfo.search}.*`, 'i')
    }
    if (pageInfo.tag) {
        searchW.tags = new RegExp(pageInfo.tag, 'i')
    };

    (async() => {
        try {
            let [count, article] = await Promise.all([
                db.articleModel.count(searchW).exec(),
                db.articleModel.find(searchW, { content: 0 })
                .skip(pageInfo.page * shownum - shownum)
                .limit(shownum).sort({ recommend: -1, _id: -1 })
                .exec()
            ])
            let allPage = Math.ceil(count / shownum)
            res.render('index', {
                data: article,
                page: allPage,
                headInfo: {
                    title: seo_tag + seo_page + config_seo.index.title,
                    keywords: config_seo.index.keywords,
                    description: config_seo.index.description
                }
            })
        } catch (e) {
            res.render('err')
        }

    })()

})

有时候访问页面就 3.jpg还是我的 服务器是 配置是 CPU: 1核 内存:1 GB (I/O优化) 1Mbps


Viewing all articles
Browse latest Browse all 14821

Trending Articles