Wrap up app.post

This commit is contained in:
Alex Hultman 2019-01-16 22:59:53 +01:00
parent d73f4e8616
commit 24fe602f50
2 changed files with 16 additions and 13 deletions

View File

@ -11,6 +11,9 @@ const app = uWS./*SSL*/App({
res.end('Hello, your url is ' + req.getUrl() + ". The param1 is " + req.getParameter(0)); res.end('Hello, your url is ' + req.getUrl() + ". The param1 is " + req.getParameter(0));
}).get('/hello', (res, req) => { }).get('/hello', (res, req) => {
res.end('Hej, du är på url: ' + req.getUrl()); 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('/*', { }).ws('/*', {
compression: 0, compression: 0,
maxPayloadLength: 16 * 1024 * 1024, maxPayloadLength: 16 * 1024 * 1024,

View File

@ -123,19 +123,18 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
args.GetReturnValue().Set(args.Holder()); args.GetReturnValue().Set(args.Holder());
} }
// todo: all other methods, in particular post! /* This method wraps get, post and all http methods */
template <typename APP> template <typename APP, typename F>
void uWS_App_get(const FunctionCallbackInfo<Value> &args) { void uWS_App_get(F f, const FunctionCallbackInfo<Value> &args) {
APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0); APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0);
NativeString nativeString(args.GetIsolate(), args[0]); NativeString nativeString(args.GetIsolate(), args[0]);
// todo: make it UniquePersistent
Persistent<Function> *pf = new Persistent<Function>(); Persistent<Function> *pf = new Persistent<Function>();
pf->Reset(args.GetIsolate(), Local<Function>::Cast(args[1])); pf->Reset(args.GetIsolate(), Local<Function>::Cast(args[1]));
//Persistent<Function, CopyablePersistentTraits<Function>> p(isolate, Local<Function>::Cast(args[1])); (app->*f)(std::string(nativeString.getData(), nativeString.getLength()), [pf](auto *res, auto *req) {
app->get(std::string(nativeString.getData(), nativeString.getLength()), [pf](auto *res, auto *req) {
HandleScope hs(isolate); HandleScope hs(isolate);
Local<Object> resObject = HttpResponseWrapper::getResInstance<APP>(); Local<Object> resObject = HttpResponseWrapper::getResInstance<APP>();
@ -215,16 +214,17 @@ void uWS_App(const FunctionCallbackInfo<Value> &args) {
appTemplate->InstanceTemplate()->SetInternalFieldCount(1); appTemplate->InstanceTemplate()->SetInternalFieldCount(1);
/* get, post */
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "get"), FunctionTemplate::New(isolate, [](auto &args) {
uWS_App_get<APP>(&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>(&APP::post, args);
}));
// Get and all the Http methods /* ws, listen */
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "get"), FunctionTemplate::New(isolate, uWS_App_get<APP>));
// Ws
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "ws"), FunctionTemplate::New(isolate, uWS_App_ws<APP>)); appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "ws"), FunctionTemplate::New(isolate, uWS_App_ws<APP>));
// Listen
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "listen"), FunctionTemplate::New(isolate, uWS_App_listen<APP>)); appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "listen"), FunctionTemplate::New(isolate, uWS_App_listen<APP>));
// Instantiate and set intenal pointer // Instantiate and set intenal pointer