NodeJS同时/异步生成性能视图

时间:2013-01-26 04:21:13

标签: javascript node.js coffeescript express ejs

我是NodeJS的新手,我对异步和并发执行IO操作的性能优势感到兴奋,因此总请求时间更接近执行最慢IO操作所需的时间而不是所有时间的总和IO操作。

然而,所有的回调都有点混乱,我想尽我所能让我的代码尽可能干。

我有一个简单的'宣传册'类型5页网站,可以为任何正常的页面请求加载标题+正文和页脚视图,但只加载一个AJAX请求的主体(使用get参数'ajaxify') - 结果内容被注入客户端页面的主体。

我正在寻找一些关于如何在处理并发/异步IO操作时管理代码设计的技巧,并在所有IO操作完成后形成响应。

这是我第一次使用NodeJS模块(用coffeescript编写 - 我也提供了编译的javascript)

是否存在常用的此类问题的任何类型的设计模式?

os                    = require 'os'
url                   = require 'url'
html_minifier   = require 'html-minifier'

render_views = (req, res, body_view, code = 200) ->

  complete = (arg) ->

    response  = arg.response
    code      = arg.code

    if query.hasOwnProperty 'ajaxify'
      res.send html_minifier.minify response.body,
        collapseWhitespace: true
        removeComments: true,
        code
    else
      if response.header != '' and response.body != '' and response.footer != ''
        res.send html_minifier.minify response.header + response.body + response.footer,
          collapseWhitespace: true
          removeComments: true,
          code

  query = url.parse(req.url, true).query

  response = 
    header: ''
    body:   ''
    footer: ''

  if query.hasOwnProperty 'ajaxify'

    code = 200
    res.render body_view, {}, (err, html) ->
      response.body = html
      complete
        response: response
        code:     code

  else
    res.render body_view, {}, (err, html) ->
      response.body = html
      complete
        response: response
        code:     code

    res.render 'view_header', {}, (err, html) -> 
      response.header = html
      complete 
        response: response
        code:     code

    res.render 'view_footer', {}, (err, html) -> 
      response.footer = html
      complete 
        response: response
        code:     code


exports.index = (req, res) ->
  render_views(req, res, 'view_index')

exports.services = (req, res) ->
  render_views(req, res, 'view_services')

exports.about = (req, res) ->
  render_views(req, res, 'view_about')

exports.blog = (req, res) ->
  render_views(req, res, 'view_blog')

exports.contact = (req, res) ->
  render_views(req, res, 'view_contact')

exports.error = (req, res) ->
  render_views(req, res, 'view_error', '404')

定期编译javascript:

    // Generated by CoffeeScript 1.4.0
(function() {
  var html_minifier, os, render_views, url;

  os = require('os');

  url = require('url');

  html_minifier = require('html-minifier');

  render_views = function(req, res, body_view, code) {
    var complete, query, response;
    if (code == null) {
      code = 200;
    }
    complete = function(arg) {
      var response;
      response = arg.response;
      code = arg.code;
      if (query.hasOwnProperty('ajaxify')) {
        return res.send(html_minifier.minify(response.body, {
          collapseWhitespace: true,
          removeComments: true
        }, code));
      } else {
        if (response.header !== '' && response.body !== '' && response.footer !== '') {
          return res.send(html_minifier.minify(response.header + response.body + response.footer, {
            collapseWhitespace: true,
            removeComments: true
          }, code));
        }
      }
    };
    query = url.parse(req.url, true).query;
    response = {
      header: '',
      body: '',
      footer: ''
    };
    if (query.hasOwnProperty('ajaxify')) {
      code = 200;
      return res.render(body_view, {}, function(err, html) {
        response.body = html;
        return complete({
          response: response,
          code: code
        });
      });
    } else {
      res.render(body_view, {}, function(err, html) {
        response.body = html;
        return complete({
          response: response,
          code: code
        });
      });
      res.render('view_header', {}, function(err, html) {
        response.header = html;
        return complete({
          response: response,
          code: code
        });
      });
      return res.render('view_footer', {}, function(err, html) {
        response.footer = html;
        return complete({
          response: response,
          code: code
        });
      });
    }
  };

  exports.index = function(req, res) {
    return render_views(req, res, 'view_index');
  };

  exports.services = function(req, res) {
    return render_views(req, res, 'view_services');
  };

  exports.about = function(req, res) {
    return render_views(req, res, 'view_about');
  };

  exports.blog = function(req, res) {
    return render_views(req, res, 'view_blog');
  };

  exports.contact = function(req, res) {
    return render_views(req, res, 'view_contact');
  };

  exports.error = function(req, res) {
    return render_views(req, res, 'view_error', '404');
  };

}).call(this);

1 个答案:

答案 0 :(得分:2)

您可以使用req.xhr(或通过检查X-Requested-With标头)来简化您的代码(假设您正在使用express)。这样你可以删除所有查询字符串...

替换

query.hasOwnProperty('ajaxify')

req.xhr

或者,如果你愿意,你可以将逻辑直接移动到视图中......只需将if(!req.xhr)等同于页眉/页脚,它就会在那里完成。在这种情况下,你的所有render_views方法都会调用res.render并进行html缩小(使其更简单)。