Download as:
Rating : ⭐⭐⭐⭐⭐
Price: $10.99
Language:EN
Pages: 10

Request params request body and merges them

path: "/upload",
settings: {
ip: "127.0.0.1",
...

routes: [
{
path: "/api", <= here name is route level entity
use: [awesomeMiddleware],
}
]
}
}

than we can ignore handler keyword and that function can act as handler itself.

module.exports = {
name: "math",
actions: {
// Only one function so it can work as handler function itself and we can omit handler function add(ctx) {
return Number(ctx.params.a) + Number(ctx.params.b);
},
// Normal definition with other properties. In this case the `handler` function is required!

Multipart parameters

In order to access the files passed by multipart-form these specific fields can be used inside the action:

1. One key difference between Broker and service is, broker doesn't have settings property where as service does.

2. Metadata are a small data that can be used as storage inside both broker and service. In both Broker and service

####### method 1 ##################

// Define mixin in authorize.mixin.js

A service action can be called by many ways.

1. Broker can call using broker.call , broker.call(actionName, params, opts); 2. Service can call using ctx.call(action_name , null , param)

All three ctx.params, ctx.meta and ctx.local are properties of ctx object.

1. When you call an action or emit an event, the broker creates a Context instance that contains all request information and passes it to the action/event handler as a single argument.

To declare action hook, follow the steps.

1. First decide whether it will be before, after or error hooks , eg. select one out of before, after or error 2. Now wrap whatever you have selected in step 1 inside a hooks object.

"my_awesome_Action" : ["getmeAfunction" , "getmeBfunction"]

4. SO final code structure will be like this
hooks: {
before: {
"*": "resolveLoggedUser",
"my_awesome_Action" : ["getmeAfunction" , "getmeBfunction"] }
}

Route hooks: The route has before & after call hooks. You can use it to set ctx.meta, access req.headers or modify the response data.

You can easily use Action hooks to modify data such as add timestamps, hash user’s passwords or remove sensitive info) before or after saving the data in DB.

How dyte is using moleculer js , here is link https://blog.dyte.io/how-moleculerjs-powers-dyte/

There are some internal pointer in req & res objects:

req.$action is pointed to the resolved action.

req.$endpoint is pointed to the resolved action endpoint.

Multipart parameters

ctx.params is the Readable stream containing the file passed to the endpoint
ctx.meta.$params parameters from URL querystring
ctx.meta.$multipart contains the additional text form-data fields must be sent before other files fields.

},

ctx.meta.$statusMessage - set res.statusMessage.

ctx.meta.$responseType - set Content-Type in header.
ctx.meta.$responseHeaders - set all keys in header.

The metadata is also obtainable on remote nodes. It is transferred during service discovering.

module.exports = {
name: "posts",
settings: {},
metadata: {
scalable: true,
priority: 5
},

};

Access it via ctx.meta in actions handlers. It will be transferred & merged at nested calls, as well.

Metadata
Send meta information to services with meta property. Access it via ctx.meta in action handlers. Please note that in nested calls the meta is merged.

subHello(ctx) {
console.log("meta from subHello:", ctx.meta);
// Prints: { user: 'John', age: 123 }
return "hi!";
}
}
});

broker.call("mod.hello", { param: 1 }, { meta: { user: "John" } });

Copyright © 2009-2023 UrgentHomework.com, All right reserved.