Express y Node.js Que es y para que sirve

Express y Node.js Que es y para que sirve 


The absolute simplest HTTP server code is listed right on the home page of nodejs.org. While the total basics are great, of course you’re going to want to do something a little more complicated and practical. Fortunately, there is a very popular library known as Express that is meant to give you all the power of a complicated web server, but still make it simple to setup and get running.
To install Express all it takes are a few simple steps. Create a folder for your project, or go into an existing folder. If there isn’t already a node application initialized there, you can create one using npm init . The tool will prompt you through a series of questions, you should be able to just use the defaults for now, and then it will create a package.json file in your folder. This file serves as the manifest for your application. Once your project has been initialized for node, then run:
This will not only install the express package in the node_modules folder, but adding the --save flag will add the entry to your package.json file as well. This is important because you can easily distribute your application and simply reinstall any dependencies, this way you don’t have to store any of your dependencies in source control either. Later, when you want to install, you can simply run npm install inside the folder, it will use the dependencies listed in the file.
When developing even simple web pages with HTML5 features, you often need to host them via a web server rather than simply opening the index.html file in your browser. Ajax requests and other advanced features are blocked from scripts running locally by the browser’s security sandbox. It’s easy using Express to setup a simple static file server.
Those lines will setup express to serve any files within the “public” folder (note, the folder relative to the script). Any html, images, css, json, txt, etc will all be accessible via http://localhost:3000 The default page that it will look for is index.html. Now you can load things locally without running into cross domain issues (say if you wanted to open a json file locally).
Express is also handy to setup a REST API for your front end development projects. It’s possible to host your back end API of course, but it’s not always the case. Often times if you are a front end developer, the service API may not be developed by you, or even using Node.js. This doesn’t have to slow down your development however, you can setup a mock server using Express relatively quickly and easily.
You can of course respond to all of the HTTP verbs (GET, POST, PUT, DELETE, etc) as well as use query string parameters and post payloads. This way you can setup the client code to expect a certain API and simply swap out the host. You can also help out the service developer by letting them know what you are expecting on the front. With Express and Node.js you can create a fully featured and robust back end API and also do more traditional server side page rendering. It just depends on how far you want to take it.
SHARE

Oscar perez

Arquitecto especialista en gestion de proyectos si necesitas desarrollar algun proyecto en Bogota contactame en el 3006825874 o visita mi pagina en www.arquitectobogota.tk

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comentarios:

Publicar un comentario