A Hello World Example for JSON

If you change the example slightly, then a JSON object will be delivered.

  1. arangosh> db._routing.save({
  2. ........> url: "/hello/json",
  3. ........> content: {
  4. ........> contentType: "application/json",
  5. ........> body: '{"hello" : "world"}'
  6. ........> }
  7. ........> });
  8. arangosh> require("internal").reloadRouting()

Show execution results

  1. {
  2. "_id" : "_routing/9599",
  3. "_key" : "9599",
  4. "_rev" : "_ZHpYaDe--_"
  5. }

Hide execution results

Again check with your browser or cURL http://localhost:8529/hello/json

Depending on your browser and installed add-ons you will either see the JSONobject or a download dialog. If your browser wants to open an externalapplication to display the JSON object, you can change the contentType to“text/plain” for the example. This makes it easier to check the example usinga browser. Or use curl to access the server.

  1.  

Show execution results

  1. shell> curl --header 'accept: application/json' --dump - http://localhost:8529/hello/json
  2.  
  3. HTTP/1.1 200 OK
  4. content-type: application/json; charset=utf-8
  5. x-content-type-options: nosniff
  6.  
  7. {
  8. "hello" : "world"
  9. }

Hide execution results

  1.  

Show execution results

  1.  

Hide execution results

Delivering Content

There are a lot of different ways on how to deliver content. We have alreadyseen the simplest one, where static content is delivered. The fun, however,starts when delivering dynamic content.

Static Content

You can specify a body and a content-type.

  1. arangosh> db._routing.save({
  2. ........> url: "/hello/contentType",
  3. ........> content: {
  4. ........> contentType: "text/html",
  5. ........> body: "<html><body>Hello World</body></html>"
  6. ........> }
  7. ........> });
  8. arangosh> require("internal").reloadRouting()

Show execution results

  1. {
  2. "_id" : "_routing/9614",
  3. "_key" : "9614",
  4. "_rev" : "_ZHpYa0q--_"
  5. }

Hide execution results

  1.  

Show execution results

  1. shell> curl --header 'accept: application/json' --dump - http://localhost:8529/hello/contentType
  2.  
  3. HTTP/1.1 200 OK
  4. content-type: text/html
  5. x-content-type-options: nosniff
  6.  
  7. "Hello World"

Hide execution results

  1.  

Show execution results

  1.  

Hide execution results

If the content type is text/plain then you can use the short-cut

  1. {
  2. content: "Hello World"
  3. }

A Simple Action

The simplest dynamic action is:

  1. {
  2. action: {
  3. do: "@arangodb/actions/echoRequest"
  4. }
  5. }

It is not advisable to store functions directly in the routing table. It isbetter to call functions defined in modules. In the above example the functioncan be accessed from JavaScript as:

  1. require("@arangodb/actions").echoRequest

The function echoRequest is pre-defined. It takes the request objects andechos it in the response.

The signature of such a function must be

  1. function (req, res, options, next)

Examples

  1. arangosh> db._routing.save({
  2. ........> url: "/hello/echo",
  3. ........> action: {
  4. ........> do: "@arangodb/actions/echoRequest"
  5. ........> }
  6. ........> });

Show execution results

  1. {
  2. "_id" : "_routing/9628",
  3. "_key" : "9628",
  4. "_rev" : "_ZHpYa3a--_"
  5. }

Hide execution results

Reload the routing and check http:// 127.0.0.1:8529/hello/echo

You should see something like

  1. arangosh> arango.GET("/hello/echo")

Show execution results

  1. {
  2. "request" : {
  3. "authorized" : true,
  4. "user" : null,
  5. "database" : "_system",
  6. "url" : "/hello/echo",
  7. "protocol" : "http",
  8. "server" : {
  9. "address" : "127.0.0.1",
  10. "port" : 16659
  11. },
  12. "client" : {
  13. "address" : "127.0.0.1",
  14. "port" : 54598,
  15. "id" : "156596609405176"
  16. },
  17. "internals" : {
  18. },
  19. "headers" : {
  20. "authorization" : "Basic cm9vdDo=",
  21. "connection" : "Keep-Alive",
  22. "user-agent" : "ArangoDB",
  23. "host" : "127.0.0.1",
  24. "accept-encoding" : "deflate"
  25. },
  26. "requestType" : "GET",
  27. "parameters" : {
  28. },
  29. "cookies" : {
  30. },
  31. "urlParameters" : {
  32. }
  33. },
  34. "options" : {
  35. }
  36. }

Hide execution results

  1.  

Show execution results

  1.  

Hide execution results

The request might contain path, prefix, suffix, and urlParameters_attributes. _path is the complete path as supplied by the user and alwaysavailable. If a prefix was matched, then this prefix is stored in the attributeprefix and the remaining URL parts are stored as an array in suffix. If oneor more parameters were matched, then the parameter values are stored inurlParameters.

For example, if the url description is

  1. {
  2. url: {
  3. match: "/hello/:name/:action"
  4. }
  5. }

and you request the path /hello/emil/jump, then the request objectwill contain the following attribute

  1. urlParameters: {
  2. name: "emil",
  3. action: "jump"
  4. }

Action Controller

As an alternative to the simple action, you can use controllers. A controller isa module, defines the function get, put, post, delete, head,patch. If a request of the corresponding type is matched, the function will becalled.

Examples

  1. arangosh> db._routing.save({
  2. ........> url: "/hello/echo",
  3. ........> action: {
  4. ........> controller: "@arangodb/actions/echoController"
  5. ........> }
  6. ........> });

Show execution results

  1. {
  2. "_id" : "_routing/9642",
  3. "_key" : "9642",
  4. "_rev" : "_ZHpYa5y--_"
  5. }

Hide execution results

Reload the routing and check http:// 127.0.0.1:8529/hello/echo:

  1. arangosh> arango.GET("/hello/echo")

Show execution results

  1. {
  2. "request" : {
  3. "authorized" : true,
  4. "user" : null,
  5. "database" : "_system",
  6. "url" : "/hello/echo",
  7. "protocol" : "http",
  8. "server" : {
  9. "address" : "127.0.0.1",
  10. "port" : 16659
  11. },
  12. "client" : {
  13. "address" : "127.0.0.1",
  14. "port" : 54598,
  15. "id" : "156596609405176"
  16. },
  17. "internals" : {
  18. },
  19. "headers" : {
  20. "authorization" : "Basic cm9vdDo=",
  21. "connection" : "Keep-Alive",
  22. "user-agent" : "ArangoDB",
  23. "host" : "127.0.0.1",
  24. "accept-encoding" : "deflate"
  25. },
  26. "requestType" : "GET",
  27. "parameters" : {
  28. },
  29. "cookies" : {
  30. },
  31. "urlParameters" : {
  32. }
  33. },
  34. "options" : {
  35. }
  36. }

Hide execution results

  1.  

Show execution results

  1.  

Hide execution results

Prefix Action Controller

The controller is selected when the definition is read. There is a moreflexible, but slower and maybe insecure variant, the prefix controller.

Assume that the url is a prefix match

  1. {
  2. url: {
  3. match: /hello/*"
  4. }
  5. }

You can use

  1. {
  2. action: {
  3. prefixController: "@arangodb/actions"
  4. }
  5. }

to define a prefix controller. If the URL /hello/echoController is given, thenthe module @arangodb/actions/echoController is used.

If you use a prefix controller, you should make certain that no unwanted actionsare available under the prefix.

The definition

  1. {
  2. action: "@arangodb/actions"
  3. }

is a short-cut for a prefix controller definition.

Function Action

You can also store a function directly in the routing table.

Examples

  1. arangosh> db._routing.save({
  2. ........> url: "/hello/echo",
  3. ........> action: {
  4. ........> callback: "function(req,res) {res.statusCode=200; res.body='Hello'}"
  5. ........> }
  6. ........> });

Show execution results

  1. {
  2. "_id" : "_routing/9656",
  3. "_key" : "9656",
  4. "_rev" : "_ZHpYbS---_"
  5. }

Hide execution results

  1. arangosh> arango.GET("hello/echo")
  2. arangosh> db._query("FOR route IN _routing FILTER route.url == '/hello/echo' REMOVE route in _routing")
  3. arangosh> require("internal").reloadRouting()

Show execution results

  1. [object ArangoQueryCursor, count: 0, cached: false, hasMore: false]

Hide execution results

Requests and Responses

The controller must define handler functions which take a request object andfill the response object.

A very simple example is the function echoRequest defined in the module@arangodb/actions.

  1. function (req, res, options, next) {
  2. var result;
  3. result = { request: req, options: options };
  4. res.responseCode = exports.HTTP_OK;
  5. res.contentType = "application/json";
  6. res.body = JSON.stringify(result);
  7. }

Install it via:

  1. arangosh> db._routing.save({
  2. ........> url: "/echo",
  3. ........> action: {
  4. ........> do: "@arangodb/actions/echoRequest"
  5. ........> }
  6. ........> })

Show execution results

  1. {
  2. "_id" : "_routing/9670",
  3. "_key" : "9670",
  4. "_rev" : "_ZHpYbU---_"
  5. }

Hide execution results

Reload the routing and check http:// 127.0.0.1:8529/hello/echo

You should see something like

  1. arangosh> arango.GET("/hello/echo")
  2. arangosh> db._query("FOR route IN _routing FILTER route.url == '/hello/echo' REMOVE route in _routing")
  3. arangosh> require("internal").reloadRouting()

Show execution results

  1. {
  2. "error" : true,
  3. "code" : 404,
  4. "errorNum" : 404,
  5. "errorMessage" : "unknown path '/hello/echo'"
  6. }
  7. [object ArangoQueryCursor, count: 0, cached: false, hasMore: false]

Hide execution results

You may also pass options to the called function:

  1. arangosh> db._routing.save({
  2. ........> url: "/echo",
  3. ........> action: {
  4. ........> do: "@arangodb/actions/echoRequest",
  5. ........> options: {
  6. ........> "Hello": "World"
  7. ........> }
  8. ........> }
  9. ........> });

Show execution results

  1. {
  2. "_id" : "_routing/9680",
  3. "_key" : "9680",
  4. "_rev" : "_ZHpYbW---_"
  5. }

Hide execution results

You now see the options in the result:

  1. arangosh> arango.GET("/echo")
  2. arangosh> db._query("FOR route IN _routing FILTER route.url == '/echo' REMOVE route in _routing")
  3. arangosh> require("internal").reloadRouting()

Show execution results

  1. {
  2. "request" : {
  3. "authorized" : true,
  4. "user" : null,
  5. "database" : "_system",
  6. "url" : "/echo",
  7. "protocol" : "http",
  8. "server" : {
  9. "address" : "127.0.0.1",
  10. "port" : 16659
  11. },
  12. "client" : {
  13. "address" : "127.0.0.1",
  14. "port" : 54598,
  15. "id" : "156596609405176"
  16. },
  17. "internals" : {
  18. },
  19. "headers" : {
  20. "authorization" : "Basic cm9vdDo=",
  21. "connection" : "Keep-Alive",
  22. "user-agent" : "ArangoDB",
  23. "host" : "127.0.0.1",
  24. "accept-encoding" : "deflate"
  25. },
  26. "requestType" : "GET",
  27. "parameters" : {
  28. },
  29. "cookies" : {
  30. },
  31. "urlParameters" : {
  32. }
  33. },
  34. "options" : {
  35. }
  36. }
  37. [object ArangoQueryCursor, count: 0, cached: false, hasMore: false]

Hide execution results