Skip to content

A plugin for Jester that creates a json object and embeds the request object information.

License

Notifications You must be signed in to change notification settings

JohnAD/jesterjson

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction to jesterjson

ver 1.0.1

nimble repo.support

This is a plugin for the nim web framework Jester. It creates a JSON object document and pre-embeds key information such as the request details.

HOW TO USE

  1. Include the plugin j <- jsonDefault() at the top of your main routes or primary router. This will enable the plugin for the whole web site.
  2. In every route, the JsonNode variable created by the plugin is available.

EXAMPLE

import json
import jester
import jesterjson

proc namePage(j: JsonNode): string =
  if j["request"]["params"].hasKey("name"):
    result = "Hello " & j["request"]["params"]["name"].getStr
  else:
    result = "Hello stranger"

routes:
  plugin j <- jsonDefault()
  get "/test":                  # get http://127.0.0.1/test?name=Joe
    resp namePage(j)

HOW IT WORKS

A new JsonNode object is created and the following keys are setup in it:

  • j["request"] contains a subset of the request variable. Specifically:

    • j["request"]["params"] is an object contains names/values of the parameters
    • j["request"]["port"] is an integer indicating the port the request came in on
    • j["request"]["host"] is a string of the host name used to make the request
    • j["request"]["secure"] is a boolean indicating HTTPS usage or not
    • j["request"]["path"] is a string of the requests URL path name
    • j["request"]["path"] is a string describing the request method (post, get, put, etc.)
  • j["env"] is an object of the OS environment variables

  • j["commandLineParams"] contains an array of the parameters passed to the server on startup

  • j["encodedUrl"] is a base-64 hex string of the URL. Sometimes that is useful for tracking and redirection.

  • j["currentTime"] is a string contains a Time parsable version of the current time and date

  • j["rfc2822"] is a string with current Time in RFC 2822 format. See https://www.ietf.org/rfc/rfc2822.txt

Table Of Contents

  1. Introduction to jesterjson

  2. Appendices

    1. jesterjson Reference

About

A plugin for Jester that creates a json object and embeds the request object information.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages