{"id":1582,"date":"2024-08-02T07:30:38","date_gmt":"2024-08-02T05:30:38","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=1582"},"modified":"2024-08-02T07:30:39","modified_gmt":"2024-08-02T05:30:39","slug":"002-api-rest-json-server-api-falsa-local","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/lenguajes\/javascript\/javascript-practico\/16-api-rest\/002-api-rest-json-server-api-falsa-local\/","title":{"rendered":"002. API REST: JSON Server &#8211; API falsa local"},"content":{"rendered":"\n<p>En este cap\u00edtulo crearemos nuestra propia <strong>API local<\/strong> falsa con una herramienta que se llama <strong><a href=\"https:\/\/www.npmjs.com\/package\/json-server\" target=\"_blank\" rel=\"noreferrer noopener\">JSON Server<\/a>,<\/strong> para lo que tenemos que tener instalado <a href=\"https:\/\/nodejs.org\/es\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Node.js<\/strong><\/a>. Tenemos que abrir la terminal y escribir la siguiente sintaxis:<\/p>\n\n\n\n<!--more-->\n\n\n\n<pre class=\"wp-block-preformatted\">node -v<\/pre>\n\n\n\n<p>Con esta sintaxis veremos la versi\u00f3n de <strong>Node.js<\/strong> que tenemos. Lo siguiente que tenemos que hacer es instalar <strong>JSON Server<\/strong>, para ello nos vamos al enlace de <strong>JSONPlaceholder,<\/strong> cuya URL es <a href=\"https:\/\/jsonplaceholder.typicode.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/jsonplaceholder.typicode.com\/<\/a>. En esta p\u00e1gina hay un enlace que dice <strong>Powered by JSON Server + LowDB<\/strong>, este enlace nos llevar\u00e1 al <strong>GitHub<\/strong> de <strong>JSON Server<\/strong>, que no es m\u00e1s que una <strong>API<\/strong> falsa de tipo <strong>REST,<\/strong> en la cual, para instalarla nos la sugiere instalar de manera global con la siguiente instrucci\u00f3n.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">npm install -g json-server<\/pre>\n\n\n\n<p>Una vez instalado tenemos que ejecutar el comando siguiente.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">json-server --watch db.json<\/pre>\n\n\n\n<p>Y despu\u00e9s vamos a crear nuestro archivo <strong>JSON,<\/strong> llamado <em>db.json<\/em>, que va a tener la siguiente sintaxis.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{\n&nbsp; \"santos\": [\n&nbsp; &nbsp; {\n&nbsp; &nbsp; &nbsp; \"id\": 1,\n&nbsp; &nbsp; &nbsp; \"nombre\": \"Seiya\",\n&nbsp; &nbsp; &nbsp; \"constelacion\": \"Pegaso\"\n&nbsp; &nbsp; },\n&nbsp; &nbsp; {\n&nbsp; &nbsp; &nbsp; \"id\": 2,\n&nbsp; &nbsp; &nbsp; \"nombre\": \"Shiryu\",\n&nbsp; &nbsp; &nbsp; \"constelacion\": \"Drag\u00f3n\"\n&nbsp; &nbsp; },\n&nbsp; &nbsp; {\n&nbsp; &nbsp; &nbsp; \"id\": 3,\n&nbsp; &nbsp; &nbsp; \"nombre\": \"Hyoga\",\n&nbsp; &nbsp; &nbsp; \"constelacion\": \"Cisne\"\n&nbsp; &nbsp; },\n&nbsp; &nbsp; {\n&nbsp; &nbsp; &nbsp; \"id\": 4,\n&nbsp; &nbsp; &nbsp; \"nombre\": \"Shun\",\n&nbsp; &nbsp; &nbsp; \"constelacion\": \"Andr\u00f3meda\"\n&nbsp; &nbsp; },\n&nbsp; &nbsp; {\n&nbsp; &nbsp; &nbsp; \"id\": 5,\n&nbsp; &nbsp; &nbsp; \"nombre\": \"Ikki\",\n&nbsp; &nbsp; &nbsp; \"constelacion\": \"F\u00e9nix\"\n&nbsp; &nbsp; }\n&nbsp; ],\n&nbsp; \"dioses\": [\n&nbsp; &nbsp; {\n&nbsp; &nbsp; &nbsp; \"id\": 1,\n&nbsp; &nbsp; &nbsp; \"nombre\": \"Athena\",\n&nbsp; &nbsp; &nbsp; \"de\": \"Tierra\"\n&nbsp; &nbsp; },\n&nbsp; &nbsp; {\n&nbsp; &nbsp; &nbsp; \"id\": 2,\n&nbsp; &nbsp; &nbsp; \"nombre\": \"Poseid\u00f3n\",\n&nbsp; &nbsp; &nbsp; \"de\": \"Mar\"\n&nbsp; &nbsp; },\n&nbsp; &nbsp; {\n&nbsp; &nbsp; &nbsp; \"id\": 3,\n&nbsp; &nbsp; &nbsp; \"nombre\": \"Hades\",\n&nbsp; &nbsp; &nbsp; \"de\": \"Inframundo\"\n&nbsp; &nbsp; },\n&nbsp; &nbsp; {\n&nbsp; &nbsp; &nbsp; \"id\": 4,\n&nbsp; &nbsp; &nbsp; \"nombre\": \"Zeus\",\n&nbsp; &nbsp; &nbsp; \"de\": \"Cielo\"\n&nbsp; &nbsp; }\n&nbsp; ]\n}<\/pre>\n\n\n\n<p>Lo siguiente que tenemos que hacer es escribir la siguiente sintaxis.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">json-server --watch assets\/db.json<\/pre>\n\n\n\n<p>Nos mostrar\u00e1 las URLs donde podremos ver los archivos <strong>JSON.<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>localhost:3000 (es el Home)<\/li>\n\n\n\n<li>localhost:3000\/dioses<\/li>\n\n\n\n<li>localhos:3000\/santos<\/li>\n<\/ul>\n\n\n\n<p>En estas dos URLs nos mostrar\u00e1n en una los dioses y en la otra los santos, si queremos acceder a alg\u00fan Dios en particular ponemos la siguiente URL<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">localhost:3000\/dioses\/4<\/pre>\n\n\n\n<p>Mostrar\u00e1 en pantalla el dios con el id 4.<\/p>\n\n\n\n<p>En el siguiente cap\u00edtulo vamos a aprender a consumir los 4 m\u00e9todos que har\u00edan en <strong>CRUD<\/strong> a una base de datos, <strong>POST<\/strong> para insertar, <strong>GET<\/strong> para buscar, <strong>PUT<\/strong> para actualizar, y <strong>DELETE<\/strong> para eliminar, que ser\u00edan los verbos correspondientes a las operaciones del <strong>CRUD<\/strong> en una base de datos pero en <strong>HTTP basados en una arquitectura REST<\/strong>, y con el software <strong>Insomnia<\/strong> o&nbsp; <strong>Postman<\/strong> haremos las 4 operaciones a esta <strong>API<\/strong> que hemos creado.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>En este cap\u00edtulo crearemos nuestra propia API local falsa con una herramienta que se llama JSON Server, para lo que [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1577,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"class_list":["post-1582","page","type-page","status-publish","hentry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Sutil Web","author_link":"https:\/\/sutilweb.eu\/index.php\/author\/sutilweb\/"},"uagb_comment_info":0,"uagb_excerpt":"En este cap\u00edtulo crearemos nuestra propia API local falsa con una herramienta que se llama JSON Server, para lo que [&hellip;]","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1582","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/comments?post=1582"}],"version-history":[{"count":1,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1582\/revisions"}],"predecessor-version":[{"id":1583,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1582\/revisions\/1583"}],"up":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1577"}],"wp:attachment":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/media?parent=1582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}