{"id":11412,"date":"2017-02-08T13:22:00","date_gmt":"2017-02-08T12:22:00","guid":{"rendered":"https:\/\/agiliacenter.com\/?p=11412"},"modified":"2025-05-28T12:12:12","modified_gmt":"2025-05-28T11:12:12","slug":"un-servidor-http-basico-con-node-js","status":"publish","type":"post","link":"https:\/\/agiliacenter.com\/es\/un-servidor-http-basico-con-node-js\/","title":{"rendered":"Un servidor HTTP b\u00e1sico con Node.js"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"11412\" class=\"elementor elementor-11412\">\n\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-50fa540d elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"50fa540d\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-15757b58\" data-id=\"15757b58\" data-element_type=\"column\" data-settings=\"{&quot;starto_ext_is_scrollme&quot;:&quot;false&quot;,&quot;starto_ext_is_smoove&quot;:&quot;false&quot;,&quot;starto_ext_is_parallax_mouse&quot;:&quot;false&quot;,&quot;starto_ext_is_infinite&quot;:&quot;false&quot;}\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-14bf6309 elementor-widget elementor-widget-text-editor\" data-id=\"14bf6309\" data-element_type=\"widget\" data-settings=\"{&quot;starto_ext_is_scrollme&quot;:&quot;false&quot;,&quot;starto_ext_is_smoove&quot;:&quot;false&quot;,&quot;starto_ext_is_parallax_mouse&quot;:&quot;false&quot;,&quot;starto_ext_is_infinite&quot;:&quot;false&quot;}\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.agiliacenter.com\/wp-content\/uploads\/2017\/02\/nodejs-1024x768-300x225.png\"><\/a><\/li><\/ul>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-29e38fdd elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"29e38fdd\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-435c5600\" data-id=\"435c5600\" data-element_type=\"column\" data-settings=\"{&quot;starto_ext_is_scrollme&quot;:&quot;false&quot;,&quot;starto_ext_is_smoove&quot;:&quot;false&quot;,&quot;starto_ext_is_parallax_mouse&quot;:&quot;false&quot;,&quot;starto_ext_is_infinite&quot;:&quot;false&quot;}\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-1590c90c elementor-widget elementor-widget-text-editor\" data-id=\"1590c90c\" data-element_type=\"widget\" data-settings=\"{&quot;starto_ext_is_scrollme&quot;:&quot;false&quot;,&quot;starto_ext_is_smoove&quot;:&quot;false&quot;,&quot;starto_ext_is_parallax_mouse&quot;:&quot;false&quot;,&quot;starto_ext_is_infinite&quot;:&quot;false&quot;}\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>En este post hablaremos sobre c\u00f3mo configurar un\u00a0<strong>servidor HTTP b\u00e1sico con Node.js.<\/strong><\/p>\n<p><span style=\"font-size: 16px;\">Cuando llegu\u00e9 al punto donde quer\u00eda empezar con mi primera aplicaci\u00f3n Node.js \u201creal\u201d, me pregunt\u00e9 no solo c\u00f3mo la iba a programar, sino que tambi\u00e9n, como organizar mi c\u00f3digo. \u00bfNecesitar\u00e9 tenerlo todo en un archivo? Muchos tutoriales en la Web que te ense\u00f1an c\u00f3mo escribir un servidor HTTP b\u00e1sico en Node.js tienen toda la l\u00f3gica en un solo lugar. \u00bfQu\u00e9 pasa si yo quiero asegurarme que mi c\u00f3digo se mantenga legible a medida que le vaya agregando m\u00e1s cosas?<\/span><\/p>\n<p>Resulta, que es relativamente f\u00e1cil de mantener los distintos aspectos de tu c\u00f3digo separados, poni\u00e9ndolos en m\u00f3dulos.<\/p>\n<p>Esto te permite tener un archivo\u00a0<em>main<\/em>\u00a0limpio, en el cual ejecutas Node.js, y m\u00f3dulos limpios que pueden ser utilizados por el archivo\u00a0<em>main,<\/em>\u00a0entre muchos otros.<\/p>\n<p>As\u00ed que vamos a crear un archivo\u00a0<em>main<\/em>\u00a0el cual usaremos para iniciar nuestra aplicaci\u00f3n, y un archivo de m\u00f3dulo d\u00f3nde residir\u00e1 el c\u00f3digo de nuestro servidor HTTP.<\/p>\n<p>Mi impresi\u00f3n es que es m\u00e1s o menos un est\u00e1ndar nombrar a tu archivo principal como\u00a0<strong>index.js<\/strong>. Tiene sentido tambi\u00e9n que pongamos nuestro m\u00f3dulo de servidor en un archivo llamado\u00a0<strong>server.js<\/strong>.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-595aaf74 elementor-widget elementor-widget-heading\" data-id=\"595aaf74\" data-element_type=\"widget\" data-settings=\"{&quot;starto_ext_is_scrollme&quot;:&quot;false&quot;,&quot;starto_ext_is_smoove&quot;:&quot;false&quot;,&quot;starto_ext_is_parallax_mouse&quot;:&quot;false&quot;,&quot;starto_ext_is_infinite&quot;:&quot;false&quot;}\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Modulo del servidor<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-78a104f1 elementor-widget elementor-widget-text-editor\" data-id=\"78a104f1\" data-element_type=\"widget\" data-settings=\"{&quot;starto_ext_is_scrollme&quot;:&quot;false&quot;,&quot;starto_ext_is_smoove&quot;:&quot;false&quot;,&quot;starto_ext_is_parallax_mouse&quot;:&quot;false&quot;,&quot;starto_ext_is_infinite&quot;:&quot;false&quot;}\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<div class=\"fusion-fullwidth fullwidth-box nonhundred-percent-fullwidth\">\n<div class=\"fusion-builder-row fusion-row \">\n<div class=\"fusion-layout-column fusion_builder_column fusion_builder_column_1_1  fusion-one-full fusion-column-first fusion-column-last 1_1\">\n<div class=\"fusion-column-wrapper\" data-bg-url=\"\">\n<p>Empecemos con el m\u00f3dulo del servidor. Crea el archivo server.js en el directorio ra\u00edz de tu proyecto, y ll\u00e9nalo con el c\u00f3digo siguiente:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">var http = require(\"http\");\n\nhttp.createServer(function(request, response) {\n  response.writeHead(200, {\"Content-Type\": \"text\/html\"});\n  response.write(\"Hola Mundo\");\n  response.end();\n}).listen(8888);<br \/><br \/><\/pre>\n<div class=\"fusion-fullwidth fullwidth-box nonhundred-percent-fullwidth\">\n<div class=\"fusion-builder-row fusion-row \">\n<div class=\"fusion-layout-column fusion_builder_column fusion_builder_column_1_1  fusion-one-full fusion-column-first fusion-column-last 1_1\">\n<div class=\"fusion-column-wrapper\" data-bg-url=\"\">\n<p>\u00a1Eso es! Acabas de escribir un servidor HTTP activo. Prob\u00e9moslo ejecut\u00e1ndolo y teste\u00e1ndolo. Primero ejecuta tu script con Node.js:<\/p>\n<div class=\"fusion-clearfix\">\n<div class=\"fusion-fullwidth fullwidth-box nonhundred-percent-fullwidth\">\n<div class=\"fusion-builder-row fusion-row \">\n<div class=\"fusion-layout-column fusion_builder_column fusion_builder_column_1_1  fusion-one-full fusion-column-first fusion-column-last 1_1\">\n<div class=\"fusion-column-wrapper\" data-bg-url=\"\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">node server.js<\/pre>\n<div class=\"fusion-clearfix\">\n<p>Ahora, abre tu browser y ap\u00fantalo a\u00a0<a href=\"https:\/\/www.agiliacenter.com:8888\/\">https:\/\/www.agiliacenter.com:8888\/<\/a>. Esto deber\u00eda desplegar una p\u00e1gina web que diga \u201cHola Mundo\u201d.<\/p>\n<p>Interesante, \u00bfno? \u00bfQu\u00e9 tal si hablamos de qu\u00e9 est\u00e1 pasando aqu\u00ed y dejamos la pregunta de \u2018<em>c\u00f3mo organizar nuestro proyecto<\/em>\u2019 para despu\u00e9s? Prometo que volveremos a esto.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"fusion-fullwidth fullwidth-box nonhundred-percent-fullwidth\">\n<div class=\"fusion-builder-row fusion-row \">\n<div class=\"fusion-layout-column fusion_builder_column fusion_builder_column_1_1  fusion-one-full fusion-column-first fusion-column-last 1_1\">\n<div class=\"fusion-column-wrapper\" data-bg-url=\"\">\u00a0<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"fusion-fullwidth fullwidth-box nonhundred-percent-fullwidth\">\n<div class=\"fusion-builder-row fusion-row \">\n<div class=\"fusion-layout-column fusion_builder_column fusion_builder_column_1_1  fusion-one-full fusion-column-first fusion-column-last 1_1\">\n<div class=\"fusion-column-wrapper\" data-bg-url=\"\">\u00a0<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-581bc40e elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"581bc40e\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-29e5a27b\" data-id=\"29e5a27b\" data-element_type=\"column\" data-settings=\"{&quot;starto_ext_is_scrollme&quot;:&quot;false&quot;,&quot;starto_ext_is_smoove&quot;:&quot;false&quot;,&quot;starto_ext_is_parallax_mouse&quot;:&quot;false&quot;,&quot;starto_ext_is_infinite&quot;:&quot;false&quot;}\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-6fe48a65 animation elementor-widget elementor-widget-image\" data-id=\"6fe48a65\" data-element_type=\"widget\" data-settings=\"{&quot;starto_ext_is_scrollme&quot;:&quot;false&quot;,&quot;starto_ext_is_smoove&quot;:&quot;false&quot;,&quot;starto_ext_is_parallax_mouse&quot;:&quot;false&quot;,&quot;starto_ext_is_infinite&quot;:&quot;false&quot;}\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img fetchpriority=\"high\" decoding=\"async\" width=\"1440\" height=\"960\" src=\"https:\/\/agiliacenter.com\/wp-content\/uploads\/2019\/04\/workflow-and-teamwork-concepts-with-colorful-PJF2EQR.jpg\" class=\"attachment-full size-full wp-image-6335\" alt=\"\" srcset=\"https:\/\/agiliacenter.com\/wp-content\/uploads\/2019\/04\/workflow-and-teamwork-concepts-with-colorful-PJF2EQR.jpg 1440w, https:\/\/agiliacenter.com\/wp-content\/uploads\/2019\/04\/workflow-and-teamwork-concepts-with-colorful-PJF2EQR-300x200.jpg 300w, https:\/\/agiliacenter.com\/wp-content\/uploads\/2019\/04\/workflow-and-teamwork-concepts-with-colorful-PJF2EQR-1024x683.jpg 1024w, https:\/\/agiliacenter.com\/wp-content\/uploads\/2019\/04\/workflow-and-teamwork-concepts-with-colorful-PJF2EQR-768x512.jpg 768w, https:\/\/agiliacenter.com\/wp-content\/uploads\/2019\/04\/workflow-and-teamwork-concepts-with-colorful-PJF2EQR-700x466.jpg 700w, https:\/\/agiliacenter.com\/wp-content\/uploads\/2019\/04\/workflow-and-teamwork-concepts-with-colorful-PJF2EQR-440x293.jpg 440w, https:\/\/agiliacenter.com\/wp-content\/uploads\/2019\/04\/workflow-and-teamwork-concepts-with-colorful-PJF2EQR-600x400.jpg 600w\" sizes=\"(max-width: 1440px) 100vw, 1440px\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-6087df36 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"6087df36\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-68191dec\" data-id=\"68191dec\" data-element_type=\"column\" data-settings=\"{&quot;starto_ext_is_scrollme&quot;:&quot;false&quot;,&quot;starto_ext_is_smoove&quot;:&quot;false&quot;,&quot;starto_ext_is_parallax_mouse&quot;:&quot;false&quot;,&quot;starto_ext_is_infinite&quot;:&quot;false&quot;}\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-3c3bebf elementor-widget elementor-widget-html\" data-id=\"3c3bebf\" data-element_type=\"widget\" data-settings=\"{&quot;starto_ext_is_scrollme&quot;:&quot;false&quot;,&quot;starto_ext_is_smoove&quot;:&quot;false&quot;,&quot;starto_ext_is_parallax_mouse&quot;:&quot;false&quot;,&quot;starto_ext_is_infinite&quot;:&quot;false&quot;}\" data-widget_type=\"html.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<script id=\"mcjs\">!function(c,h,i,m,p){m=c.createElement(h),p=c.getElementsByTagName(h)[0],m.async=1,m.src=i,p.parentNode.insertBefore(m,p)}(document,\"script\",\"https:\/\/chimpstatic.com\/mcjs-connected\/js\/users\/75a6184da5d3774bd7ca93769\/b54d955464a1e456262be535e.js\");<\/script>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>En este post hablaremos sobre c\u00f3mo configurar un&nbsp;servidor HTTP b\u00e1sico con Node.js. Cuando llegu\u00e9 al punto donde quer\u00eda empezar con mi primera aplicaci\u00f3n Node.js \u201creal\u201d, me pregunt\u00e9 no solo c\u00f3mo la iba a programar, sino que tambi\u00e9n, como organizar mi [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":11418,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11412","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sin-categoria"],"_links":{"self":[{"href":"https:\/\/agiliacenter.com\/es\/wp-json\/wp\/v2\/posts\/11412","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/agiliacenter.com\/es\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/agiliacenter.com\/es\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/agiliacenter.com\/es\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/agiliacenter.com\/es\/wp-json\/wp\/v2\/comments?post=11412"}],"version-history":[{"count":9,"href":"https:\/\/agiliacenter.com\/es\/wp-json\/wp\/v2\/posts\/11412\/revisions"}],"predecessor-version":[{"id":13101,"href":"https:\/\/agiliacenter.com\/es\/wp-json\/wp\/v2\/posts\/11412\/revisions\/13101"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/agiliacenter.com\/es\/wp-json\/wp\/v2\/media\/11418"}],"wp:attachment":[{"href":"https:\/\/agiliacenter.com\/es\/wp-json\/wp\/v2\/media?parent=11412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/agiliacenter.com\/es\/wp-json\/wp\/v2\/categories?post=11412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/agiliacenter.com\/es\/wp-json\/wp\/v2\/tags?post=11412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}