节点。js express应用缓存/慢页加载(swig模板引擎)

时间:2022-08-24 17:36:51

I'm in the process of moving my apache site (on bluehost) to node.js (on heroku), and am noticing that it runs quite a bit slower. I'm wondering if it's a caching issue or what I could be doing wrong.

我正在将apache站点(在bluehost上)迁移到node。我注意到它运行得相当慢。我想知道这是缓存问题还是我做错了什么。

Here is the site on heroku: http://ak-web-prod.herokuapp.com/

heroku网站:http://ak- prod.herokuapp.com/

Here is the site on bluehost: http://ardentkid.com

这是bluehost的网站:http://ardentkid.com

If you notice, the page flashes white during load sometimes when navigating the site (which is why I think it might be a caching problem). I've set express config for:

如果您注意到,页面在加载时有时会在浏览站点时闪烁白色(这就是为什么我认为这可能是缓存问题)。我为:

app.enable('view cache');

doesn't seem to change anything. Anyone have any ideas?

似乎没有什么改变。谁有什么好主意吗?

Here is my app configuration

这是我的应用配置

app.configure(function(){
    app.set('config', config);
    app.set('views', __dirname + '/views');
    app.set('view engine', 'html');
    app.set('db', db);
    app.set('port', process.env.PORT || 3000);
    app.engine('.html', cons.swig);
    app.use(express.logger('dev'))
    app.use(express.favicon(__dirname + '/public/img/favicon.ico'));
    app.use(express.cookieParser())
    app.use(express.bodyParser()) //enables req.body
    app.use(express.methodOverride()) //enables app.put and app.delete (can also just use app.post)
    app.use(express.session({
        secret: 'topsecret'
        , store: new RedisStore({
              client:db
            , secret:config.db.auth
            })
    }));
    app.use(passport.initialize()) // use passport session
    app.use(passport.session())
    app.use(app.router) // routes should be at the last
});

app.configure('development', function(){
    console.log('app in development mode');
    app.use('/assets', express.static(__dirname + '/public'));
    app.use('/', express.static(__dirname + '/'));
    swig.init({root: __dirname + '/views', allowErrors: true, cache: false});
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('stage', function(){
    console.log('app in development mode');
    app.use('/assets', express.static(__dirname + '/public', { maxAge: 86400000 }));
    app.use('/', express.static(__dirname + '/', { maxAge: 86400000 }));
    swig.init({root: __dirname + '/views', allowErrors: true, cache:true});
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
    app.enable('view cache');
    app.use('/assets', express.static(__dirname + '/public', { maxAge: 86400000 }));
    app.use('/', express.static(__dirname + '/', { maxAge: 86400000 }));
    swig.init({root: __dirname + '/views', cache:true});
    app.use(express.errorHandler());
});

3 个解决方案

#1


1  

Issue was due to my redis database (on RedisToGo) not connecting properly. I didn't think this would affect the page loads, but it definitely did. Now that it's fixed, the app is speedier than ever!

问题是由于我的redis数据库(在RedisToGo上)连接不正确导致的。我不认为这会影响页面加载,但它肯定会。现在它是固定的,应用程序比以前更快了!

#2


1  

It's hard to say, but I think there might be two things affecting the load time

这很难说,但我认为可能有两件事会影响加载时间

  1. If you are using the free version of Heroku, it could be causing the slowness.
  2. 如果你使用免费版本的Heroku,它可能会导致慢速。
  3. The Headers being sent aren't allowing the browser to cache, requiring it to re-download everything. Ex.
  4. 发送的头文件不允许浏览器缓存,需要浏览器重新下载所有内容。前女友。

curl -XHEAD -v http://ak-web-prod.herokuapp.com/assets/img/logo.png

curl http://ak-web-prod.herokuapp.com/assets/img/logo.png -XHEAD - v

< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: public, max-age=0
< Content-length: 11264
< Content-Type: image/png
< Date: Sun, 31 Mar 2013 03:41:33 GMT
< Etag: "11264-1364696076000"
< Last-Modified: Sun, 31 Mar 2013 02:14:36 GMT
< X-Powered-By: Express
< Connection: keep-alive

To have your static assets served up with Cache-Control set to something besides zero, take a look at the static middleware - http://www.senchalabs.org/connect/static.html, it allows you to set the max-age. This will affect images, JS, and CSS.

要将静态资产与Cache-Control设置为非零,请查看静态中间件http://www.senchalabs.org/connect/static.html,它允许您设置max-age。这将影响图像、JS和CSS。

#3


1  

You should add this :

你应该加上这一点:

swig.setDefaults({
    cache: false,
});

#1


1  

Issue was due to my redis database (on RedisToGo) not connecting properly. I didn't think this would affect the page loads, but it definitely did. Now that it's fixed, the app is speedier than ever!

问题是由于我的redis数据库(在RedisToGo上)连接不正确导致的。我不认为这会影响页面加载,但它肯定会。现在它是固定的,应用程序比以前更快了!

#2


1  

It's hard to say, but I think there might be two things affecting the load time

这很难说,但我认为可能有两件事会影响加载时间

  1. If you are using the free version of Heroku, it could be causing the slowness.
  2. 如果你使用免费版本的Heroku,它可能会导致慢速。
  3. The Headers being sent aren't allowing the browser to cache, requiring it to re-download everything. Ex.
  4. 发送的头文件不允许浏览器缓存,需要浏览器重新下载所有内容。前女友。

curl -XHEAD -v http://ak-web-prod.herokuapp.com/assets/img/logo.png

curl http://ak-web-prod.herokuapp.com/assets/img/logo.png -XHEAD - v

< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: public, max-age=0
< Content-length: 11264
< Content-Type: image/png
< Date: Sun, 31 Mar 2013 03:41:33 GMT
< Etag: "11264-1364696076000"
< Last-Modified: Sun, 31 Mar 2013 02:14:36 GMT
< X-Powered-By: Express
< Connection: keep-alive

To have your static assets served up with Cache-Control set to something besides zero, take a look at the static middleware - http://www.senchalabs.org/connect/static.html, it allows you to set the max-age. This will affect images, JS, and CSS.

要将静态资产与Cache-Control设置为非零,请查看静态中间件http://www.senchalabs.org/connect/static.html,它允许您设置max-age。这将影响图像、JS和CSS。

#3


1  

You should add this :

你应该加上这一点:

swig.setDefaults({
    cache: false,
});