This function is meant to handle an incoming HTTP request and return an HTTP response. Let’s add a special function, which in Node.js we call a request listener. When we bind our server to this host and port, we will be able to reach our server by visiting in a local browser. Ports 80 are typically used as default ports in development, and in most cases developers will use them rather than other ports for HTTP servers. In our example, we will use port 8000 for our web server. The port is a number that servers use as an endpoint or “door” to our IP address. It’s typically the equivalent of the internal IP address 127.0.0.1 and it’s only available to the local computer, not to any local networks we’ve joined or to the internet. The value localhost is a special private address that computers use to refer to themselves. For more information on domain name concepts, take a look at our An Introduction to DNS Terminology, Components, and Concepts article. An IP address is a unique sequence of numbers that identify a machine on a network, like the internet. We may interact with a web server by entering a domain name, which is translated to an IP address by a DNS server. const host = 'localhost' const port = 8000 Īs mentioned before, web servers accept requests from browsers and other clients. Add the following line to hello.js:įirst-servers/hello.js. We start by loading the http module that’s standard with all Node.js installations. We will use nano as it’s available in the terminal: Now, create the file that will house the code: In the terminal, create a folder called first-servers: This will cover the key concepts required to set up a server, which will provide the foundation necessary to return more complex data formats like JSON.įirst, we need to set up an accessible coding environment to do our exercises, as well as the others in the article. Let’s start by creating a server that returns plain text to the user. If you’re not familiar with asynchronous programming in Node.js or the fs module for interacting with files, you can learn more with our article on How To Write Asynchronous Code in Node.js. We also make use of asynchronous programming for one of our sections.You can get started by reviewing our guide on How To Write and Run Your First Program in Node.js. To get started, be sure you’re familiar with the basics of Node.js. The Node.js platform supports creating web servers out of the box.To install this on macOS or Ubuntu 18.04, follow the steps in How to Install Node.js and Create a Local Development Environment on macOS or the Installing Using a PPA section of How To Install Node.js on Ubuntu 18.04. This tutorial uses Node.js version 10.19.0. Ensure that Node.js is installed on your development machine.You will build web servers that can return JSON data, CSV files, and HTML web pages. In this tutorial, you will learn how to build web servers using the http module that’s included in Node.js. Having both the frontend and backend together like this reduces the effort it takes to make a web server, which is a major reason why Node.js is a popular choice for writing back-end code. Node.js allows developers to use JavaScript to write back-end code, even though traditionally it was used in the browser to write front-end code. Code that handles network requests from your browser or communicates with the database is primarily managed by back-end code. Back-end code is concerned with how data is exchanged, processed, and stored. Front-end code is concerned with how the content is presented, such as the color of a navigation bar and the text styling. This software generally falls into two categories: frontend and backend. A web server receives HTTP requests from a client, like your browser, and provides an HTTP response, like an HTML page or JSON from an API.Ī lot of software is involved for a server to return a webpage. That computer you are talking to via the internet is a web server. When you view a webpage in your browser, you are making a request to another computer on the internet, which then provides you the webpage as a response. The author selected the COVID-19 Relief Fund to receive a donation as part of the Write for DOnations program.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |