V
V
valera0962020-02-05 08:46:13
Node.js
valera096, 2020-02-05 08:46:13

Why do I get a 204 response code instead of 200, although the data should be?

I send a request to the server and in the handler I get data from the database through sequelize
, here is the fragment code:

const products = {
   getAll: async () => {
     let goods = await Product.findAll({raw:true})     
      .catch(err => console.log(`DataBase Error ${err}`))

    console.log(`goods`);
    console.log(goods);

    return goods;
  },


The console outputs the following:
spoiler
[nodemon] restarting due to changes...
[nodemon] starting `node --use-strict ./src/app.js`
Server listen on localhost:80
Executing (default): SELECT 1+1 AS result
Executing (default): SELECT "id", "title", "description", "price", "pathImage" FROM "products" AS "product";
  app:db Connection has been established successfully. +0ms
goods
[
  {
    id: 1,
    title: 'newItem',
    description: 'testItem',
    price: 100,
    pathImage: null
  }
]


When requested, the browser outputs the following to the console:
res = 
appServiceData.js:10 {data: "", status: 204, statusText: "No Content", headers: {…}, config: {…}, …}
appServiceData.js:11 res.data =

??????????????????????

Client code:
spoiler
class AppServiceData {

  async getResourse(url) {
    const res = await axios.get(url);
    console.log('res = ');
    console.log(res);
    console.log('res.data = ');
    console.log(res.data);
    return res.data;
  };

  async getProducts() {
    let res =  await this.getResourse('http://localhost/api/products');
    return res;
  };
};


But if you explicitly send some object in return, then everything is fine
spoiler
res = 
appServiceData.js:10 {data: "goods", status: 200, statusText: "OK", headers: {…}, config: {…}, …}
appServiceData.js:11 res.data = 
appServiceData.js:12 goods


What could be the reason why no Content?

Edit:

Server code:
const path = require('path');
const Koa = require('koa');
const serve = require('koa-static');
const err = require('./middleware/error');
const router = require('./middleware/api/routes');
const debug = require('debug')('app');
const send = require('koa-send');

const staticDir = path.resolve(__dirname, '..', '..', 'public');
const app = new Koa();

app.use(err);
app.use(serve(staticDir));
app.use(router.routes());
app.use(router.allowedMethods());
app.use(async ctx => await send(ctx, 'index.html', { root: staticDir }));

app.listen(3000, () => {
  console.log('Server listen on localhost:80');
});


Router:
const router = new Router();
const koaBody = convert(KoaBody());

  //**products endpoints */
  .post('/api/products', async ctx => {
    ctx.body = await products.addProduct();
  })
  .get('/api/products', async ctx => {
     ctx.body = await products.getAll();  
  })

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly, 2020-02-05
@vshvydky

callback that executes koa, and the fact that you use it is clear only from your comment, it receives the context argument as input

async (ctx) => {
    ctx.body = 'твой ответ'
}

since you complete your handler without data mapping, you get 204 (no content)
I think the problem is not in koa.

A
Alexey Ukolov, 2020-02-05
@alexey-m-ukolov

data: "", status: 204
data: "goods", status: 200

if you explicitly send some object in return, then everything is fine

Your question can be rephrased like this: "why, if you send content, then the status is 200, and if you do not send - 204". The answer to it, I think, is obvious.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question