那些年
在nodejs上踩过的坑(二)

继风(朱佳墩) @继风Jordan

@2012-11-29 @night of node

大纲

浅拷贝

你写过类似的代码吗?

 
function copy(from) {
  var to = {};
  for (var key in from) {
    to[key] = from[key];
  }
  return to;
}

解决方案

escape

connect中escape的实现

代码地址:https://github.com/senchalabs/connect/blob/master/lib/utils.js

测试方法

中间件如何测试


app.use(function (req, res, next) {
  //do some thing
});

解决方案

supertest


  var app = express();

  app.get('/user', function(req, res){
    res.send(201, { name: 'tobi' });
  });

  describe('GET /users', function() {
    it('respond with json', function(done){
      request(app)
      .get('/user')
      .set('Accept', 'application/json')
      .expect('Content-Type', /json/)
      .expect(200, done);
    });
  }); 

supertest bug

body只判断一次


  describe('GET /users', function() {
    it('respond with json', function(done){
      request(app)
      .get('/user')
      .set('Accept', 'application/json')
      .expect('Content-Type', /json/)
      .expect(/body content 1/)
      .expect(/body content 2/)
      .expect(200, done);
    });
  }); 

bug已修复(苏千)

Thank you

/

#