Saltar navegación

Activa JavaScript para disfrutar de los vídeos de la Mediateca.

Back end vs Front end - Vídeo 8 - Contenido educativo

Ajuste de pantalla

El ajuste de pantalla se aprecia al ver el vídeo en pantalla completa. Elige la presentación que más te guste:

Subido el 9 de julio de 2024 por Mario S.

115 visualizaciones

Creamos rutas en el backend (servidor). express.Routes(). Creamos controladores para cada una de las rutas. Probamos el cliente Thunder Client.

Descargar la transcripción

well in this video we are going to make the routes as I have told you in the previous video in the 00:00:01
previous video we did with the function of mongoose the scheme and then we passed it to that model 00:00:08
then we had to make the routes and associate a controller to those routes so that it does something 00:00:16
when we enter those routes the routes are the different links to which we can 00:00:21
to which we can access in our back-end ok then we are going to have each of the 00:00:27
properties that we want the properties I am referring to on the one hand register on the other 00:00:37
side log in and then give up for example tasks or delete tasks or update 00:00:43
tasks ok then each of those actions each of those tasks will go to a different route 00:00:51
we cannot do everything on the same route, especially because then to that route we can put 00:00:59
different security degrees, it is worth that I will also teach you how it is done and well, I will draw you how it is 00:01:10
done and then you will get more into depth, then to do this we are going to go to our 00:01:20
root folder is that for that we had created it and there we are going to create a file that I am going to call 00:01:26
out.routes.js, okay, well, you know that you can call it as you want, always making 00:01:33
sure that it is what you are saving in each one, these are very standard names and you see 00:01:46
how as soon as I put routers this icon is going to take me, well, then, well, I follow 00:01:51
because the names that are usually given when you are looking for technical documentation 00:02:00
is worth what is the first thing I do routers is a function that in this case is from the express module 00:02:06
express we already have it loaded is the first thing we have loaded to be able to activate our server 00:02:14
is worth to be able to raise the server service at port 4000 is worth as you have here 00:02:22
below is worth then we did with express express also allows us the functionality of creating 00:02:30
those routes and associate a route to one to a variable to an arrow function so that it executes 00:02:36
what we think is appropriate as developers then the first thing I do as 00:02:44
always is the import here I am going to put router then it gives me all the direction 00:02:51
says import router between keys from express ok well we would already have the first thing that is the 00:03:06
second thing I am going to do create a variable that then I am going to export it 00:03:13
that I am going to call it for example router in lowercase this is going to be equal to router 00:03:21
to the function that we have imported from the express module, then once I have done this 00:03:27
I can already start to generate my routes then I am going to put the router 00:03:39
point well when we are working with web development the methods to access the 00:03:46
the server you know or should know that there are several for example you can have the guest you can 00:03:57
have post the put the pad the delete is worth with the get what we do is recover a data from the 00:04:07
server this is always looked at from the front to the back it is worth from the client to the server then 00:04:17
what we do is recover a data with the post what we do is create new resources with the put 00:04:23
what we do is replace a resource that is already there that is existing with an update is worth 00:04:34
with the patch we do not replace a resource that is the resource that we already have in the backend in the server 00:04:44
is to update it, okay, but it is not replaced by a new one, that is, it is not the same as it is not exactly 00:04:51
the same as the put and the delay because to erase a resource of our database, it is usually 00:05:02
used, it is worth among the ones that we usually use the get the post the put is worth and the delay 00:05:10
well then I these routes that I want to do are of that point post is worth because because what I want 00:05:17
is to create a new resource is worth what I want in these links is to create a new resource that in this 00:05:27
case is going to be for example I am going to open the parenthesis says the path the string is worth what I tell you is how 00:05:36
what route I am going to create then I for example here I am going to create the register ok 00:05:47
and we are also going to create the router post point 00:05:55
if then I do not use the post and I use the put or anything of this I am going to 00:06:01
modify it ok but now I am going to put both as post and here I am going to put it in login 00:06:07
ok well in this way I have these two routes that right now I do nothing more than create them 00:06:12
ok but they do nothing, that is, I am not associating any function, no controller, I am not 00:06:23
doing absolutely nothing with them, I am only passing the express router to recognize me 00:06:30
these routes ok then this has two things if it is if I am playing with the express module 00:06:37
you will have to say in app that I use this variable these routes ok then this router variable 00:06:44
that is why I have created it for me what I am going to do is export it also then we are going to export 00:06:54
export from faun ok I am going to export it to be able to use it from any 00:07:01
site ok then what else are we going to do well now I could come directly to the 00:07:17
ppjs and say well in the ppjs I am going to import import 00:07:24
router ok and it already tells me ok import the route of this from the file that we are creating 00:07:37
voucher routers out to no longer recognize it perfect and now I tell express to use it 00:07:49
I tell you app.yus and we pass it the router router is worth a point is worth in this way I am already 00:07:56
telling the express to our back in our part of the server that it uses it is good 00:08:09
to not always be putting the same names you can put any other name here for 00:08:18
example instead of router that I can put out a little to continue with the same nomenclature that 00:08:25
I am using routes is worth and then here what we are going to pass is the out route is worth in this 00:08:35
case in our outroutes file I am only exporting this variable when I export 00:08:48
and I call it in app it is already taking router and it is putting it into me and it is assigning it 00:09:00
to this alias that I am putting here out routers ok ok to see if everything works 00:09:08
well and there is no problem we can take it and start it ok then it tells us in sc src index js 00:09:17
in js now I have a problem js index ok it is because I have not yet finished doing 00:09:27
the route issue well now we continue to test it well then I have the route I have 00:09:38
the register I have the login we have exported the router now this if I put a comma here you 00:09:48
veis que lo que os pide es que queréis qué es lo que tiene que hacer este o que se tiene que hacer 00:09:57
cuando el usuario el front accede a este link vale entonces tengo que crearme esos controladores para 00:10:07
that I have this folder of controllers then we are going to create those controllers in this 00:10:18
case I am going to do a function for each of the links that we have only because all register and 00:10:27
login because we are going to create two functions then I am going to create another file within the controller 00:10:33
that I am going to call it out dot controller dot js out controller dot js here what I am going to do is 00:10:39
those two functions are worth then in these two functions it will be with it will be one that is 00:10:58
register, for example, this is going to be equal to an arrow function, it is worth that it is going to do something and we are going to 00:11:11
put with login it is going to be equal to the arrow function that it is going to do because right now the only thing 00:11:34
we are going to do is put a message ok well then we have the register and we have the login ok 00:11:48
when we are with the web development when we have a back and a front 00:11:58
there are two parameters that are always the network and the network is worth the network is the request ok it is the request 00:12:08
and the network is the answer, then we always use or do not use these parameters, you have to 00:12:16
pass them, you have to pass them because in the end the server does have to send some 00:12:28
type of answer or the front has to send some data to the server, it is worth then 00:12:33
always put those two input parameters the network that are the parameters that come 00:12:39
ok and the network that is the answer that gives the back in the front ok ok and what am I going to tell you inside 00:12:48
each of these functions because on the one hand with the network parameter ok I'm going to tell you 00:12:57
this look if I show it we come here ok in the technical documentation later 00:13:09
you see you have the network send that what you say is send a response of various types is worth 00:13:24
then that all that information everything that I am saying now the network is to be able to send 00:13:30
un string al front vale lo tengo que usar desde aquí y si le pulsáis aquí encima tenéis pues los 00:13:36
diferentes ejemplos tenéis toda la información que os puede hacer falta vale y todo lo que 00:13:46
podéis enviar desde el res vale entonces es importante insisto muy muy muy importante en 00:13:52
that you manage the documentation of everything we are using, okay, we go back to our visual 00:14:00
studio code and here what I do is put the point network as I want to send a string it is worth 00:14:08
and here we are going to put it in quotation marks for registering for example something like this 00:14:16
ok I close here and here we are going to do we are going to put this is login because 00:14:24
ok ok and I would already have these two functions that I want to export them ok then I can't 00:14:40
forget to put the export here in front of the two functions arrow the export and the export is worth 00:14:51
and what do I do with this because in the router 00:15:06
vale lo que hago es importar esas dos funciones esto es para importar las 00:15:10
import llaves le hemos llamado a una registra y a la otra login vale y ya nos dice desde el 00:15:21
file and such does not recognize it and what do we do here now because those functions that we have created register 00:15:35
and logic ok ok ok and here now what we are going to do is try that those two links 00:15:51
works for us ok let's see this error that is giving us in index j s index I have not touched it 00:16:08
so it has to be in the app and 00:16:19
we load the express we use morgan and we use the variable that we have imported 00:16:24
the road.js, I forget, okay, it's already restarted and it seems that everything is fine now 00:16:35
we are going to try it to try it we are going to install a client to be able to simulate that from 00:16:46
ok then for the client that we are going to use is one that is usually used thunder 00:16:57
ok this one although there are several ok but well I use this thunder client you can use 00:17:09
whatever you want we install it we already have it the thunder client icon appears you press it 00:17:16
new request, this would be the front part and here what we are going to tell him is 00:17:26
the method that we are going to use is going to be post, which is how we have put the routes post post 00:17:37
vale esto fuera el post y aquí le vamos a meter él local host 4.000 bajos 4.000 lo vale 00:17:43
and here it appears in what would be the response, if we put the register or intro, 00:18:21
as you want, registering, so it is working for us well, it is recognizing the routes well, it 00:18:40
is recognizing the controllers that we have associated with those routes well, that in this case I am only 00:18:46
visualizing a string with the res send, the res that is the response that I am 00:18:51
sending to the front, to the client, which in this case is the Thunder client 00:18:57
and up to here we already have, on the one hand, the scheme and the model made with mongoose 00:19:02
and we have made the different routes where we are going to associate each of 00:19:07
the controllers and from there to develop what we want it to do, which will be 00:19:12
access to databases and that's it. 00:19:17
Idioma/s:
es
Idioma/s subtítulos:
en es
Autor/es:
Mario S.
Subido por:
Mario S.
Licencia:
Dominio público
Visualizaciones:
115
Fecha:
9 de julio de 2024 - 19:29
Visibilidad:
Público
Centro:
IES VILLABLANCA
Duración:
00′ 12″
Relación de aspecto:
1.78:1
Resolución:
1920x1080 píxeles
Tamaño:
44.88 MBytes

Del mismo autor…

Ver más del mismo autor


EducaMadrid, Plataforma Educativa de la Comunidad de Madrid

Plataforma Educativa EducaMadrid