New API Controllers

Generate a new API controller for any api version you have created.

Generating the controllers

Generate a controller to v1

By default, generating a controller will go v1 however you can specify the version you want the controller in.

$ node-rails new-api-controller <NAME-OF-YOUR-CONTROLLER>

Generate a controller to other api versions

When generating a controller in anyother api version, provide the option with a number for your api version that exists.

$ node-rails new-api-controller <NAME-OF-YOUR-CONTROLLER> --version=<NUMBER-OF-YOUR-API-VERSION>

What Was Generated

The Controller directory is a circular import for any file inside of it.

The controller files inside of this directory must be an exported function and the name of your file is the name of the function. You must give unique names to all your files in this directory so none of them are overwrited. This will happen naturally with a file system.

Example: Creating a API Controller

Generating a Users Controller and get function

$ node-rails new-api-controller users$ touch ./controllers/v1/users/get.js

Writing the get function

const { User } = require('../../../models');

module.exports = async (req, res) => {

try {
const { id } = req.params;
const resp = await User.fineOne({ where: { id } })
res.status(200).send(resp);
} catch (error) {
res.status(400).send({ error });
}

};

Alias Shorthands

Shorthand for New API Controller

$ node-rails nac <NAME-OF-YOUR-CONTROLLER>