Skip to content

1. Getting Started (Recommended)

Arnav Gupta edited this page Feb 5, 2018 · 1 revision

Installation

Install JAGAPI server stuff

npm install jagapi jagapi-sequelize

Install DB drivers

npm install pg@6 pg-hstore

Basic Directory Structure

src
├── handlers   # dir
├── models     # dir
├── api.js     # create config here
└── server.js  # start server here

API Setup File

Create a jsonapi config object NOTE: We are using a config.js file for common information

const config = require('../config')
const jsonAPI = require('jagapi')

jsonAPI.setConfig({
    graphiql: true, // Whether we want to use graphiql
    jsonapi: true, // Whether we want to use jsonapi
    protocol: 'http',
    hostname: config.SERVER.HOST,
    port: config.SERVER.PORT,
    base: 'api',  //API base path (not required, defaults to / )
    meta: {
        description: config.INFO.DESC
    },
    swagger: { // Swagger metadata object (that is part of all responses)
        title: config.INFO.TITLE,
        version: '0.1.0',
        description: config.INFO.DESC,
        contact: {
            name: config.INFO.CONTACT.NAME,
            email: config.INFO.CONTACT.EMAIL,
            url: config.INFO.CONTACT.URL,
        },
        license: {
            name: 'MIT',
            url: 'http://opensource.org/licenses/MIT'
        }
    }
})

Create empty authenticate and metrics middelwares

jsonAPI.authenticate((req, cb) => {
    return cb()
})

jsonAPI.metrics.on('data', data => {
    debug('metrics')(data)
})

Export the server and start and close functions

exports = module.exports = {
    server: jsonAPI.getExpressServer(),
    start: jsonAPI.start,
    close: jsonAPI.close
}

Server Setup

Optional (Swagger UI)

npm install swagger-ui-express 

Optional (GraphiQL console)

npm install express-graphiql-toolbox

Start the server

const api = require('./api')
const swaggerUi = require('swagger-ui-express')
const graphiql = require('express-graphiql-toolbox')

/**
 * To serve a Swagger UI Console
 */
api.server.use('/docs', swaggerUi.serve,
    swaggerUi.setup(
        null, true, null, null, null,
        '/api/swagger.json', 'qBounty API Docs'
    )
)

/**
 * To serve a GraphiQL console
 */
api.server.use('/graphiql', graphiql({endpoint: '/api/'}))

api.start()