简体   繁体   中英

How to return the API response while using claudia-api-builder?

I am using Claudia-api-builder with Sequelize and facing issues while returning the response from an API

app.js:

const ApiBuilder = require('claudia-api-builder');
const app = new ApiBuilder();

const citizenPersonalDetails = require('./app/controller/cont.citizenpersonaldetails');

app.get('/citizens', function (req, res) {
  citizenPersonalDetails.getAll(req, res);
})

module.exports = app

when I am returning the response like:

getAll: function (req, res) {
        citizenPersonalDetails.findAll({})
            .then(citizenPersonalDetails => {
                if (citizenPersonalDetails.length === 0) {
                    return res.status(200).json(citizenPersonalDetails)
                } else {
                    return res.status(200).json(citizenPersonalDetails)
                }
            }).catch(error => {
                console.log("==== ERROR ====", error);
            });
    }

This gives me error: res.status is not a function

In Claudia documentation, it mentions only the request object:

https://claudiajs.com/claudia-api-builder.html

from documentation:

var ApiBuilder = require('claudia-api-builder'),
    api = new ApiBuilder(),
    superb = require('superb');

module.exports = api;

api.get('/greet', function (request) {
    return request.queryString.name + ' is ' + superb();
});

is there any response object for Claudia? what is the correct way to return the response?

I am using claudia-local-api to the APIS test locally.

There is no response 2nd parameter (like you have in Express.js).

It appears that there is an ApiResponse that you can use to return a custom response and customer header.

But if you already have this working using Express.js, maybe skip claudia-api-builder altogether and just use claudia to run Express.js app in AWS Lambda

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM