小编lei*_*iiv的帖子

Node.js正确使用bodyParser中间件

我是node.js的新手并且表达并且已经尝试了一段时间.现在我对与解析请求体相关的快速框架的设计感到困惑.来自快递的官方指南:

app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(logErrors);
app.use(clientErrorHandler);
app.use(errorHandler);
Run Code Online (Sandbox Code Playgroud)

在设置了所有中间件之后,我们添加了我们想要处理的路由:

app.post('/test', function(req, res){ 
  //do something with req.body      
});
Run Code Online (Sandbox Code Playgroud)

这种方法的问题在于,在检查路由有效性之前,将首先解析所有请求主体.解析无效请求的主体似乎效率很低.更重要的是,如果我们启用上传处理:

app.use(express.bodyParser({uploadDir: '/temp_dir'}));
Run Code Online (Sandbox Code Playgroud)

任何客户端都可以通过上传任何文件来轰炸服务器(通过向任何路由/路径发送请求!!),所有这些都将被处理并保存在'/ temp_dir'中.我无法相信这种默认方法正在被广泛推广!

我们当然可以在定义路径时使用bodyParser函数:

app.post('/test1', bodyParser, routeHandler1);
app.post('/test2', bodyParser, routeHandler2);
Run Code Online (Sandbox Code Playgroud)

甚至可能在处理路径的每个函数中解析主体.但是,这很麻烦.

是否有更好的方法可以仅对所有有效(已定义)路由使用express.bodyParser,并且仅在选定路由上使用文件上载处理功能,而无需重复执行大量代码?

connect node.js express

27
推荐指数
1
解决办法
1万
查看次数

在g ++上使用不完整类型无效

我有两个相互依赖的类:

class Foo; //forward declaration

template <typename T>
class Bar {
  public:
    Foo* foo_ptr;
    void DoSomething() {
      foo_ptr->DoSomething();
    }
};

class Foo {
  public:
    Bar<Foo>* bar_ptr;
    void DoSomething() {
      bar_ptr->DoSomething();
    }
};
Run Code Online (Sandbox Code Playgroud)

当我用g ++编译它时,它给出了"无效使用不完整类型"的错误,但它在MSVC 10中编译得很好.是否有可能在将声明和定义保存在一个头文件中时解决这个问题?(没有cpp文件)如果标准中不允许这样,那么这个MSVC"bug"或"feature"是哪一个?

c++ g++ class

8
推荐指数
1
解决办法
9534
查看次数

我们如何在子类中键入或重新定义模板化嵌套类?

考虑以下:

template <typename T>
class Base {
  public:
    template <typename U>
    class Nested { };
};

template <typename T>
class Derived : public Base<T> {
  public:
    //How do we typedef of redefine Base<T>::Nested?
    using Base<T>::Nested; //This does not work
    using Base<T>::template<typename U> Nested; //Cannot do this either
    typedef typename Base<T>::template<typename U> Nested Nested; //Nope..

    //now we want to use the Nested class here
    template <typename U>
    Class NestedDerived : public Nested { };

    //or like this:
    Nested<int> nestedVar; // obviously …
Run Code Online (Sandbox Code Playgroud)

c++ templates

7
推荐指数
1
解决办法
2064
查看次数

如何设计一个类,其中user/caller具有使用自定义类提供自定义行为的选项

我遇到一个问题,我希望有一个类,其行为可以由另一个类定制,例如,Foo的构造函数接受某种类的类的参数:

class Bar { //The default class that define behavior
};

template <typename T = Bar>
class Foo {
  public:
    Foo(T* t = 0) t_(t) {
      if (t_ == 0) t_ = new T();
    }
    ~Foo() { 
      delete t_;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,如果有人在客户端代码中使用Foo:

Foo foo;
Run Code Online (Sandbox Code Playgroud)

一切都好.但是,如果我们想提供自定义类:

class Bar1 { };

Foo<Bar1> foo(new Bar1()); // This is OK
Bar1 b;
Foo<Bar1> foo(&b); // Error, b is not dynamically allocated
Run Code Online (Sandbox Code Playgroud)

我可以使用任何设计模式来防止这种错误吗?或者,是否有任何技术或语义,Foo类的用户可以选择/指定谁拥有条形对象?例如,上面的Foo析构函数可以是这样的:

    ~Foo() { 
      if (t_ is owned by this object) delete t_;
    } …
Run Code Online (Sandbox Code Playgroud)

c++

2
推荐指数
1
解决办法
194
查看次数

标签 统计

c++ ×3

class ×1

connect ×1

express ×1

g++ ×1

node.js ×1

templates ×1