mirror of https://gitee.com/bigwinds/arangodb
301 lines
6.1 KiB
Plaintext
301 lines
6.1 KiB
Plaintext
!SECTION A Hello World Example for JSON
|
|
|
|
If you change the example slightly, then a JSON object will be delivered.
|
|
|
|
```js
|
|
arangosh> db._routing.save({
|
|
url: "/hello/json",
|
|
content: {
|
|
contentType: "application/json",
|
|
body: "{ \"hello\" : \"world\" }"
|
|
}
|
|
});
|
|
arangosh> require("internal").reloadRouting()
|
|
```
|
|
|
|
Again check with your browser http:// localhost:8529/hello/json
|
|
|
|
Depending on your browser and installed add-ons you will either see the JSON
|
|
object or a download dialog. If your browser wants to open an external
|
|
application to display the JSON object, you can change the *contentType* to
|
|
*"text/plain"* for the example. This makes it easier to check the example using
|
|
a browser. Or use *curl* to access the server.
|
|
|
|
```js
|
|
bash> curl "http://127.0.0.1:8529/hello/json" && echo
|
|
{ "hello" : "world" }
|
|
```
|
|
|
|
!SECTION Delivering Content
|
|
|
|
There are a lot of different ways on how to deliver content. We have already
|
|
seen the simplest one, where static content is delivered. The fun, however,
|
|
starts when delivering dynamic content.
|
|
|
|
!SUBSECTION Static Content
|
|
|
|
You can specify a body and a content-type.
|
|
|
|
```js
|
|
{
|
|
content: {
|
|
contentType: "text/html",
|
|
body: "<html><body>Hello World</body></html>"
|
|
}
|
|
}
|
|
```
|
|
|
|
If the content type is *text/plain* then you can use the short-cut
|
|
|
|
```js
|
|
{
|
|
content: "Hello World"
|
|
}
|
|
```
|
|
|
|
!SUBSECTION A Simple Action
|
|
|
|
The simplest dynamic action is:
|
|
|
|
```js
|
|
{
|
|
action: {
|
|
do: "org/arangodb/actions/echoRequest"
|
|
}
|
|
}
|
|
```
|
|
It is not advisable to store functions directly in the routing table. It is
|
|
better to call functions defined in modules. In the above example the function
|
|
can be accessed from JavaScript as:
|
|
|
|
```js
|
|
require("org/arangodb/actions").echoRequest
|
|
```
|
|
|
|
The function *echoRequest* is pre-defined. It takes the request objects and
|
|
echos it in the response.
|
|
|
|
The signature of such a function must be
|
|
|
|
```js
|
|
function (req, res, options, next)
|
|
```
|
|
|
|
*Examples*
|
|
|
|
```js
|
|
arangosh> db._routing.save({
|
|
url: "/hello/echo",
|
|
action: {
|
|
do: "org/arangodb/actions/echoRequest"
|
|
}
|
|
});
|
|
```
|
|
|
|
Reload the routing and check http:// 127.0.0.1:8529/hello/echo
|
|
|
|
You should see something like
|
|
|
|
```js
|
|
{
|
|
"request": {
|
|
"path": "/hello/echo",
|
|
"headers": {
|
|
"accept-encoding": "gzip, deflate",
|
|
"accept-language": "de-de,de;q=0.8,en-us;q=0.5,en;q=0.3",
|
|
"connection": "keep-alive",
|
|
"content-length": "0",
|
|
"host": "localhost:8529",
|
|
"user-agent": "Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0"
|
|
},
|
|
"requestType": "GET",
|
|
"parameters": { }
|
|
},
|
|
"options": { }
|
|
}
|
|
```
|
|
|
|
The request might contain *path*, *prefix*, *suffix*, and *urlParameters*
|
|
attributes. *path* is the complete path as supplied by the user and always
|
|
available. If a prefix was matched, then this prefix is stored in the attribute
|
|
*prefix* and the remaining URL parts are stored as an array in *suffix*. If one
|
|
or more parameters were matched, then the parameter values are stored in
|
|
*urlParameters*.
|
|
|
|
For example, if the url description is
|
|
|
|
```js
|
|
{
|
|
url: {
|
|
match: "/hello/:name/:action"
|
|
}
|
|
}
|
|
```
|
|
|
|
and you request the path */hello/emil/jump*, then the request object
|
|
will contain the following attribute
|
|
|
|
```js
|
|
urlParameters: {
|
|
name: "emil",
|
|
action: "jump"
|
|
}
|
|
```
|
|
|
|
!SUBSECTION Action Controller
|
|
|
|
As an alternative to the simple action, you can use controllers. A controller is
|
|
a module, defines the function *get*, *put*, *post*, *delete*, *head*,
|
|
*patch*. If a request of the corresponding type is matched, the function will be
|
|
called.
|
|
|
|
*Examples*
|
|
|
|
```js
|
|
arangosh> db._routing.save({
|
|
url: "/hello/echo",
|
|
action: {
|
|
controller: "org/arangodb/actions/echoController"
|
|
}
|
|
});
|
|
```
|
|
|
|
!SUBSECTION Prefix Action Controller
|
|
|
|
The controller is selected when the definition is read. There is a more
|
|
flexible, but slower and maybe insecure variant, the prefix controller.
|
|
|
|
Assume that the url is a prefix match
|
|
|
|
```js
|
|
{
|
|
url: {
|
|
match: /hello/*"
|
|
}
|
|
}
|
|
```
|
|
|
|
You can use
|
|
|
|
```js
|
|
{
|
|
action: {
|
|
prefixController: "org/arangodb/actions"
|
|
}
|
|
}
|
|
```
|
|
|
|
to define a prefix controller. If the URL */hello/echoController* is given, then
|
|
the module *org/arangodb/actions/echoController* is used.
|
|
|
|
If you use a prefix controller, you should make certain that no unwanted actions
|
|
are available under the prefix.
|
|
|
|
The definition
|
|
|
|
```js
|
|
{
|
|
action: "org/arangodb/actions"
|
|
}
|
|
```
|
|
is a short-cut for a prefix controller definition.
|
|
|
|
!SUBSECTION Function Action
|
|
|
|
You can also store a function directly in the routing table.
|
|
|
|
*Examples*
|
|
|
|
```js
|
|
arangosh> db._routing.save({
|
|
url: "/hello/echo",
|
|
action: {
|
|
callback: "function(req,res) {res.statusCode=200; res.body='Hello'}"
|
|
}
|
|
});
|
|
```
|
|
!SUBSECTION Requests and Responses
|
|
|
|
The controller must define handler functions which take a request object and
|
|
fill the response object.
|
|
|
|
A very simple example is the function *echoRequest* defined in the module
|
|
*org/arangodb/actions*.
|
|
|
|
```js
|
|
function (req, res, options, next) {
|
|
var result;
|
|
|
|
result = { request: req, options: options };
|
|
|
|
res.responseCode = exports.HTTP_OK;
|
|
res.contentType = "application/json";
|
|
res.body = JSON.stringify(result);
|
|
}
|
|
```
|
|
|
|
Install it via:
|
|
|
|
```js
|
|
arangosh> db._routing.save({
|
|
url: "/echo",
|
|
action: {
|
|
do: "org/arangodb/actions/echoRequest"
|
|
}
|
|
});
|
|
```
|
|
|
|
Reload the routing and check http:// 127.0.0.1:8529/hello/echo
|
|
|
|
You should see something like
|
|
|
|
```js
|
|
{
|
|
"request": {
|
|
"prefix": "/hello/echo",
|
|
"suffix": [
|
|
"hello",
|
|
"echo"
|
|
],
|
|
"path": "/hello/echo",
|
|
"headers": {
|
|
"accept-encoding": "gzip, deflate",
|
|
"accept-language": "de-de,de;q=0.8,en-us;q=0.5,en;q=0.3",
|
|
"connection": "keep-alive",
|
|
"content-length": "0",
|
|
"host": "localhost:8529",
|
|
"user-agent": "Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0"
|
|
},
|
|
"requestType": "GET",
|
|
"parameters": { }
|
|
},
|
|
"options": { }
|
|
}
|
|
```
|
|
|
|
You may also pass options to the called function:
|
|
|
|
```js
|
|
arangosh> db._routing.save({
|
|
url: "/echo",
|
|
action: {
|
|
do: "org/arangodb/actions/echoRequest",
|
|
options: {
|
|
"Hello": "World"
|
|
}
|
|
}
|
|
});
|
|
```
|
|
|
|
You should now see the options in the result.
|
|
|
|
```js
|
|
{
|
|
"request": {
|
|
...
|
|
},
|
|
"options": {
|
|
"Hello": "World"
|
|
}
|
|
}
|
|
```` |