Skip to content

Instantly share code, notes, and snippets.

@JPDevM

JPDevM/blog.md Secret

Created May 14, 2021 10:35
Show Gist options
  • Save JPDevM/c4fb64e8cd9cbeb9464bb464336cc11a to your computer and use it in GitHub Desktop.
Save JPDevM/c4fb64e8cd9cbeb9464bb464336cc11a to your computer and use it in GitHub Desktop.

Workflow to create backend with NodeJS

.frameworks_logos

Install NodeJS and ExpressJS

https://gist.github.com/c4726b59d8a80d4e8bad419a48218d7b

https://gist.github.com/c6f950dc2f093f80eb33a011ebe35164

Create Folder structure.

Create too the README.md, LICENCE & .gitignore.

.Folder-structure

Set Express.

In main file. e.g.: app.js

https://gist.github.com/201ded35b4c89db2b06433ca5559003b

https://gist.github.com/14fc45d5c75f3d1ba5d267e504008187

Definition of MVC entities.

In this example, 3 entities will be used.

  • Subscriptions.
  • Promotions.
  • Users.
MVC: Create Routes.

Express documentation: https://expressjs.com/es/guide/routing.html

In main file. e.g.: app.js

https://gist.github.com/41b43826c2b4ca44f65507d1d0fe0c1e

Test some routes:

· http://localhost:5000/ · http://localhost:5000/subscriptions · http://localhost:5000/users · http://localhost:5000/sdfg

MVC: Modularize and create routes files (entities).

A ROUTER is an "entity". BREAD: Browse, Read, Edit, Add, Delete. If preparing the backend to consume as API with the frontend, static is not defined as entity.

.Routes-files

In main file. e.g.: app.js

https://gist.github.com/cdbac3179be1d2cdc5d30f1a6e8ee1f9

In the router file. e.g.:subscriptionsRouter.js

Idem in the other routers entities. e.i.: staticRouter.js, promotionsRouter.js, usersRouter.js.

https://gist.github.com/bfdf856eecef09f93ce36a8de953ce76

MVC: BREAD entities.

7 paths if views are rendered. 5 routes if we prepare the backend to consume it via API: 1 BROWSE - See all 3 EDIT - Edit one (edit form) (only for views) 4 EDIT - Edit one 5 ADD - Add one (creation form) (only for views) 6 ADD - Add one 7 DELETE - Delete one 2 READ - See one

BREAD_routes

In the router file. e.g.:subscriptionsRouter.js

Idem in the other routers entities. e.i.: staticRouter.js, promotionsRouter.js, usersRouter.js.

https://gist.github.com/57a8dae6df24ad5c0ee18665639654e4

MVC: Create controllers files.

Controllers-files

MVC: Tell routes the controller method.
BREAD_view

In the router file. e.g.:subscriptionsRouter.js

Idem in the other routers entities. e.i.: staticRouter.js, promotionsRouter.js, usersRouter.js.

https://gist.github.com/006a5eae261f9a353c053e833a146e61

MVC: Create the controller standard methods.

In the router file. e.g.:subscriptionsControllers.js

Idem in the other routers entities. e.i.: staticRouter.js, promotionsRouter.js, usersRouter.js,

https://gist.github.com/ec7caf86123afe889b08c84081b77b3a

In this part of the process the control methods should show the results. The database is in charge of displaying the results.
  • [Edit the methods of the controller from the database.md](./Edit the methods of the controller from the database.md).

  • [Edit the methods of the controller using JSON files.md](./Edit the methods of the controller usin JSON files.md).

Another option is to send the views directly instead of the API, in this case with EJS as template engine.

  • [MVC: Tell the controller to show the views](./MVC Tell the controller to show the views.md).
Install thrid-party dependencies.

.Thrid-party-dependencies-logos

Rest API: Create a Json file in src for each entity with its data.

Rest API: Set the Json Objet.

In main file. e.g.: app.js

https://gist.github.com/e4380a90156b0578cd524f6c9549922d

Rest API: Set how Express handles information in forms.

In main file. e.g.: app.js

https://gist.github.com/bc986d387cae559a9fab450408fdb12e

Json Models lo utilizo porque como uso Json, saco los métodos más comunes afuera json Models I use it because as I use Json, I pull the most common methods outside

las rutas o controladores o sus métodos (no sé) ahora envian respuestas Json por que son APIs routes or controllers or their methods (I don't know) now send Json responses because they are APIs

<<<<<<< HEAD https://www.npmjs.com/package/cors

Extraigo las variables de desarrollo en archivos de producción .env y hago la documentación .env.example

requiero lainstalo la librería https://www.npmjs.com/package/dotenv

utilizo process.env para extraer las vaiables las pongo utilizo en app.js y ahora van a ser dínámicas


librería para el logueo social. http://www.passportjs.org/


Si hago pública la API. ver el tema de los token en la API (ver ejemplo en promotionsController) (el método serch se define desde el controlador, añadirlo en el lugar que va en el readme.md) Tengo que ver cuantas request pueden hacer por usuario.

Documentación https://medium.com/zero-equals-false/using-cors-in-express-cac7e29b005b e31ab6eab708556c3c99110afdac185d0a669e12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment