steve dunning
steve dunning

Reputation: 163

Loopback 4 REST Controller path returns NotFoundError 404

Created a REST Controller with CRUD functions object via the CLI using lb4 controller media pointing to an existing MediaRepository for an existing Entity Media model both of which were generated using the lb4 CLI as well.

A MediaController class was created with all of the REST routes for /media*

The /ping route works fine so I looked for any special routing configuration for it to see if there might be a config messing for /media but nothing was obvious.

An HTTP Get request to /media response with a web page having the following content:

  <h1>NotFoundError</h1>
  <h2><em>404</em> Endpoint &#34;GET /media&#34; not found.</h2>

There is probably some fundamental configuration or setup that needs to happen but I am just not seeing it.

MediaController class

import {
  Count,
  CountSchema,
  Filter,
  repository,
  Where,
} from '@loopback/repository';
import {
  post,
  param,
  get,
  getFilterSchemaFor,
  getWhereSchemaFor,
  patch,
  put,
  del,
  requestBody, Request, RestBindings, ResponseObject
} from '@loopback/rest';
import { Media } from '../models';
import { MediaRepository } from '../repositories';

export class MediaController {
  constructor(
    @repository(MediaRepository)
    public mediaRepository: MediaRepository,
  ) { }

  @post('/media', {
    responses: {
      '200': {
        description: 'Media model instance',
        content: { 'application/json': { schema: { 'x-ts-type': Media } } },
      },
    },
  })
  async create(@requestBody() media: Media): Promise<Media> {
    return await this.mediaRepository.create(media);
  }

  @get('/media/count', {
    responses: {
      '200': {
        description: 'Media model count',
        content: { 'application/json': { schema: CountSchema } },
      },
    },
  })
  async count(
    @param.query.object('where', getWhereSchemaFor(Media)) where?: Where<Media>,
  ): Promise<Count> {
    return await this.mediaRepository.count();
  }

  @get('/media', {
    responses: {
      '200': {
        description: 'Array of Media model instances',
        content: {
          'application/json': {
            schema: { type: 'array', items: { 'x-ts-type': Media } },
          },
        },
      },
    },
  })
  async find(
    @param.query.object('filter', getFilterSchemaFor(Media)) filter?: Filter<Media>,
  ): Promise<Media[]> {
    return await this.mediaRepository.find(filter);
  }

  @patch('/media', {
    responses: {
      '200': {
        description: 'Media PATCH success count',
        content: { 'application/json': { schema: CountSchema } },
      },
    },
  })
  async updateAll(
    @requestBody() media: Media,
    @param.query.object('where', getWhereSchemaFor(Media)) where?: Where<Media>,
  ): Promise<Count> {
    return await this.mediaRepository.updateAll(media, where);
  }

  @get('/media/{id}', {
    responses: {
      '200': {
        description: 'Media model instance',
        content: { 'application/json': { schema: { 'x-ts-type': Media } } },
      },
    },
  })
  async findById(@param.path.string('id') id: string): Promise<Media> {
    return await this.mediaRepository.findById(id);
  }

  @patch('/media/{id}', {
    responses: {
      '204': {
        description: 'Media PATCH success',
      },
    },
  })
  async updateById(
    @param.path.string('id') id: string,
    @requestBody() media: Media,
  ): Promise<void> {
    await this.mediaRepository.updateById(id, media);
  }

  @put('/media/{id}', {
    responses: {
      '204': {
        description: 'Media PUT success',
      },
    },
  })
  async replaceById(
    @param.path.string('id') id: string,
    @requestBody() media: Media,
  ): Promise<void> {
    await this.mediaRepository.replaceById(id, media);
  }

  @del('/media/{id}', {
    responses: {
      '204': {
        description: 'Media DELETE success',
      },
    },
  })
  async deleteById(@param.path.string('id') id: string): Promise<void> {
    await this.mediaRepository.deleteById(id);
  }
}

Upvotes: 0

Views: 1065

Answers (1)

steve dunning
steve dunning

Reputation: 163

So I set lb4 aside for a while while I evaluated other frameworks.

Came back to my lb4 demo project today. No changes to anything since then. Started the application.

npm run start

Browsed to localhost:3000/media

To my surprise it returned a json response. Now my response array was empty and it should have returned something as there were documents in the mongodb datasource but that is a separate issue to figure out.

Upvotes: 1

Related Questions