diff --git a/examples/HelloWorld.js b/examples/HelloWorld.js index f738711..ee8863b 100644 --- a/examples/HelloWorld.js +++ b/examples/HelloWorld.js @@ -11,6 +11,9 @@ const app = uWS./*SSL*/App({ res.end('Hello, your url is ' + req.getUrl() + ". The param1 is " + req.getParameter(0)); }).get('/hello', (res, req) => { res.end('Hej, du är på url: ' + req.getUrl()); +}).post('/hello', (res, req) => { + // todo: receive the data here! + res.end('Thanks for the data!'); }).ws('/*', { compression: 0, maxPayloadLength: 16 * 1024 * 1024, diff --git a/src/AppWrapper.h b/src/AppWrapper.h index e23889e..1d218e1 100644 --- a/src/AppWrapper.h +++ b/src/AppWrapper.h @@ -123,19 +123,18 @@ void uWS_App_ws(const FunctionCallbackInfo &args) { args.GetReturnValue().Set(args.Holder()); } -// todo: all other methods, in particular post! -template -void uWS_App_get(const FunctionCallbackInfo &args) { +/* This method wraps get, post and all http methods */ +template +void uWS_App_get(F f, const FunctionCallbackInfo &args) { APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0); NativeString nativeString(args.GetIsolate(), args[0]); + // todo: make it UniquePersistent Persistent *pf = new Persistent(); pf->Reset(args.GetIsolate(), Local::Cast(args[1])); - //Persistent> p(isolate, Local::Cast(args[1])); - - app->get(std::string(nativeString.getData(), nativeString.getLength()), [pf](auto *res, auto *req) { + (app->*f)(std::string(nativeString.getData(), nativeString.getLength()), [pf](auto *res, auto *req) { HandleScope hs(isolate); Local resObject = HttpResponseWrapper::getResInstance(); @@ -215,16 +214,17 @@ void uWS_App(const FunctionCallbackInfo &args) { appTemplate->InstanceTemplate()->SetInternalFieldCount(1); + /* get, post */ + appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "get"), FunctionTemplate::New(isolate, [](auto &args) { + uWS_App_get(&APP::get, args); + })); - /* Most used functions will be get, post, ws, listen */ + appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "post"), FunctionTemplate::New(isolate, [](auto &args) { + uWS_App_get(&APP::post, args); + })); - // Get and all the Http methods - appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "get"), FunctionTemplate::New(isolate, uWS_App_get)); - - // Ws + /* ws, listen */ appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "ws"), FunctionTemplate::New(isolate, uWS_App_ws)); - - // Listen appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "listen"), FunctionTemplate::New(isolate, uWS_App_listen)); // Instantiate and set intenal pointer