Express + Mongoose backend

views1758
Difficulty:

The backend of our simple Nuxt.the application's js is in the folder server. The main application file is index.js. In it, we connect Express and Mongoose, then connect to the database. Next comes the connection of routes that are associated with the Page model. At the end of the file, we connect Nuxt.js as middleware and use nuxt.render to render content.

The model folder contains model schema Page. We list the fields that our dynamic pages will consist of. For example, you can immediately specify the type of field, the requirements for its presence and uniqueness. It is convenient to add an automatically added document creation date via Date.now.

The routes folder contains routes that are associated with the Page model. In our simplest application, this is:

  • output of all pages,
  • output of one page,
  • creating a page,
  • page refresh,
  • deleting a page.

We search for one page that we want to display, update or delete by its url. They are unique to us and are transmitted in the request path from the frontend. Output this task to a separate getPage function, which performs a search. If the page is not found, a 404 error is returned.

Author: Aleksandr Dergunov
Published: 3/27/2021