Skip to content

DEFRA/flood-xws-subscription-api

Repository files navigation

contact-api

XWS contact API

Environment variables

name description required default valid notes
NODE_ENV Node environment no development,test,production
PORT Port number no 3000

Prerequisites

Node v12+

Running the application

$ node index.js

Getting started

Clone this repo and run through the checklist above.

Check the server is running by pointing your browser to http://localhost:3000

Project structure

Here's the default structure for your project files.

  • server
    • plugins
    • routes
    • config.js
    • index.js (Exports a function that creates a server)
  • test
  • README.md
  • index.js (startup server)

Config

The configuration file for the server is found at server/config.js. This is where to put any config and all config should be read from the environment. The final config object should be validated using joi and the application should not start otherwise.

A table of environment variables should be maintained in this README.

Plugins

hapi has a powerful plugin system and all server code should be loaded in a plugin.

Plugins live in the server/plugins directory.

Logging

The hapi-pino plugin is used and configured in server/plugins/logging

Routes

Incoming requests are handled by the server via routes. Each route describes an HTTP endpoint with a path, method, and other properties.

Routes are found in the server/routes directory and loaded using the server/plugins/router.js plugin.

Hapi supports registering routes individually or in a batch. Each route file can therefore export a single route object or an array of route objects.

A single route looks like this:

{
  method: 'GET',
  path: '/hello-world',
  options: {
    handler: (request, h) => {
      return 'hello world'
    }
  }
}

There are lots of route options, here's the documentation on hapi routes

Tasks

Build tasks are created using simple shell scripts or node.js programs. The default ones are found in the bin directory.

The task runner is simply npm using npm-scripts.

We chose to use this for simplicity but there's nothing to stop you adding gulp, grunt or another task runner if you prefer.

The predefined tasks are:

  • npm run lint (Runs the lint task using standard.js)
  • npm run unit-test (Runs the lab tests in the /test folder)
  • npm test (Runs the lint task then the unit-tests)

Testing

lab and code are used for unit testing.

See the /test folder for more information.

Linting

standard.js is used to lint both the server-side and client-side javascript code.

It's defined as a build task and can be run using npm run lint.

About the license

The Open Government Licence (OGL) was developed by the Controller of Her Majesty's Stationery Office (HMSO) to enable information providers in the public sector to license the use and re-use of their information under a common open licence.

It is designed to encourage use and re-use of information freely and flexibly, with only a few conditions.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published