Hey there. This time we will see how to write a web server using node.js and http
.
What do I need to know?
What will I learn?
This is a guide for beginners so it comes with some theory. I will not dig a lot this time (assuming you already know what a server is) but if you don't care about it, you can go straight to the action.
For this tutorial, we are going to use the node http module, so let's see what is it.
HTTP
The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative, hypermedia information systems.
I really love Wikipedia's definitions XD. Let's try to clarify this concept.
A web application is stored on a piece of hardware with its respective software, which allows communication with other pieces of software called clients——This is a really basic definition about what a web server is but is enough for us now.
This "server" needs a set of rules and permissions to allow communication between it and the clients, and that's what HTTP is there for.
HTTP is the main web protocol. When we enter a website, our browser, which acts as a client, sends a request through an URL, then the server process that petition and sends a response.
In the case of node.js, we need to configure our server's behavior. It doesn't work as Apache or similar servers, and that's what we are going to learn next.
The action
This time we don't need to install anything. the http module comes as default with node.js so the first thing we are going to do is create our project directory and file to run our server. Open the command line and write the following.
mkdir server
cd server
touch server.js
// On windows
type nul > server.js
Open the server.js
file with your favorite text editor and add the following.
const http = require('http')
As I already said, http comes with node.js so we don't need to install it. With this line, we load the module into the http
constant.
const host = '127.0.0.1'
const port = 3000
As we are going to run our server on our local machine, we set our host referring localhost
with its IP, and for a convention, we are going to use port 3000
.
const server = http.createServer((req, res) => {
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')
res.end('Hello, world!')
})
We are using the createServer
method from http
and we are passing around a callback with the parameters req
and res
. req
is an object with the petition information, and res
, an object with the response.
We are setting the statusCode
200 which is a standard response for successful HTTP requests.
With the setHeaders
method, we are adding the content type of the response, and to finish, we close the connection with a message using the method end
.
We will learn about HTTP headers and status codes and messages in future posts. By now this is enough. Let's continue.
server.listen(port, host, () => {
console.log(`Listening on http://${host}:${port}`)
})
The method listen
will receive the port, the host and a callback function that we will use to log a message in the console and it will execute the server.
Let's try it. In the command line, write.
node ./server
You should see this
Listening on http://127.0.0.1:3000
Now go to http://127.0.0.1:3000 and you will see the message "Hello, World!"
That's it. Now you have your first server running in node'js. In the next post, we will start using the Express
framework to build our first app.
If you think I missed something, let me know in the comments below. Please, like and share if you found this useful. See you next time.
Top comments (0)