0

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

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

我有一个简单的 'brochure' 类型 5 页面网站,它为任何正常页面请求加载页眉 + 正文 + 页脚视图,但仅加载 AJAX 请求的正文(带有获取参数 '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);
4

1 回答 1

2

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

代替

query.hasOwnProperty('ajaxify')

req.xhr

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

于 2013-01-26T09:32:48.173 回答