Search…
Route
Routes are overridden by name and weight, and sorted by priority (desc), then by weight (desc).
1
class MyRoute {
2
3
@route('GET c_340_ping', {
4
name: 'c_340_get',
5
weight: 1
6
})
7
static get({ req, res, body, next, runtime }) {
8
9
return true
10
11
}
12
13
@route({
14
weight: 1,
15
method: 'POST',
16
name: 'c_340_post',
17
path: 'c_340_ping',
18
acl: 'role.administrator'
19
})
20
post({ req, res, body, next, runtime }) {
21
22
return body('myData')
23
24
}
25
26
}
Copied!

@route(options)

Arguments
  • options { Object } Options object
    • name { String }
    • type { String }
    • principal { String }
    • environment { String = "*" }
    • weight { Number = 0 }
    • method { String }
    • path { String }
    • authValidation { Object }
    • urlEncoded { String }
    • plainText { String }
    • apiKey { String }
    • priority { Number }

@route(routeIdentity, options)

Arguments
  • routeIdentity { String } A string defining the route method using the format "<method> <path>"
  • options { Object } Options object
    • name { String }
    • type { String }
    • principal { String }
    • environment { String = "*" }
    • weight { Number = 0 }
    • authValidation { Object }
    • urlEncoded { String }
    • plainText { String }
    • apiKey { String }
    • priority { Number }

Method Options

  • methodOptions { Object } Options passed to the method
    • req { Object }
    • res { Object }
    • body { Object }
    • next { Function }
    • runtime { Object }
      • name { String }
      • type { String }
      • principal { String }
      • environment { String = "*" }
      • weight { Number = 0 }
      • configuration { Object }
        • method { String }
        • path { String }
        • authValidation { Object }
        • urlEncoded { String }
        • plainText { String }
        • apiKey { String }
        • priority { Number }
        • acl { String }
      • metadata { Object }
        • resource { String }
        • className { String }
        • methodName { String }
        • static { Boolean }
        • loc { Object }
          • line { String }
          • column { String }
body(key, value)
Accessor for the script API's body
  • key { string } Key associated to a useful value
  • value { Object | number | string | boolean = undefined } If defined, will create an association between the key and value
Example
1
const data = body('myData')
Copied!
next(err)
Calls the API route's next method.
1
script.api.route.next(err)
Copied!
  • err { Object = null } Error object

Route Layering

1
class Ping extends CortexObject {
2
3
// run first no api key set, will use that of the caller.
4
@log({ traceError: true })
5
@route('* *', { priority: 999 })
6
static count({ req, res, next, runtime }) {
7
8
try {
9
res.setHeader('Content-Type', 'application/x-ndjson')
10
} catch(err) {}
11
12
res.write(JSON.stringify({
13
route: `${runtime.configuration.method} /routes/${runtime.configuration.path}`,
14
key: req.client.key,
15
principal: script.principal.email,
16
resource: runtime.metadata.resource
17
})+'\n')
18
19
next()
20
}
21
22
// run second, but only if pingCount is 1. pins the app to c_dashboard
23
@log({ traceError: true })
24
@route('POST pingCount/1', {
25
apiKey: 'c_dashboard',
26
priority: 2
27
})
28
static countAgain({ req, res, next, runtime }) {
29
30
try {
31
res.setHeader('Content-Type', 'application/x-ndjson')
32
} catch(err) {}
33
34
res.write(JSON.stringify({
35
route: `${runtime.configuration.method} /routes/${runtime.configuration.path}`,
36
key: req.client.key,
37
principal: script.principal.email,
38
resource: runtime.metadata.resource
39
})+'\n')
40
41
next()
42
43
}
44
45
// run last
46
@log({ traceError: true })
47
@route('POST pingCount/:num', { priority: 1 })
48
static countOnceMore({ req, res, runtime }) {
49
50
try {
51
res.setHeader('Content-Type', 'application/x-ndjson')
52
} catch(err) {}
53
54
res.write(JSON.stringify({
55
route: `${runtime.configuration.method} /routes/${runtime.configuration.path}`,
56
key: req.client.key,
57
principal: script.principal.email,
58
resource: runtime.metadata.resource
59
})+'\n')
60
61
}
62
63
}
Copied!

Result

mdctl ndjson
mdctl tail
1
$> mdctl api POST /routes/pingCount --ndjson
2
{"route":"* /routes/*","key":"MedableWebAppClientKey","principal":"[email protected]","resource":"script#type(library).name(c_ping_library)[email protected] 57:2"}
3
{"route":"post /routes/pingCount/1","key":"kw30gTF8kXDIiPJYemlDBj","principal":"[email protected]","resource":"script#type(library).name(c_ping_library)[email protected] 71:2"}
4
{"route":"post /routes/pingCount/:num","key":"kw30gTF8kXDIiPJYemlDBj","principal":"[email protected]","resource":"script#type(library).name(c_ping_library)[email protected] 86:2"}
Copied!
1
$> mdctl tail
2
{"timestamp":"2020-01-28T23:46:45.398Z","level":"info","message":[{"className":"Ping","methodName":"countAgain","lineNumber":70,"ms":6.93505859375}]}
3
{"timestamp":"2020-01-28T23:46:45.618Z","level":"info","message":[{"className":"Ping","methodName":"count","lineNumber":56,"ms":6.615966796875}]}
4
{"timestamp":"2020-01-28T23:46:45.828Z","level":"info","message":[{"className":"Ping","methodName":"countOnceMore","lineNumber":85,"ms":3.302001953125}]}
Copied!
Last modified 2mo ago
Copy link