DEV Community 👩‍💻👨‍💻

Yoshio Hasegawa
Yoshio Hasegawa

Posted on

Creating a Node Server Without Express

This article is meant to be a very simple introduction to the client-server model and, show you how to create a simple server using Node. Before we start looking at code showing how to create a server, it's important to first understand what a server is.

What is a server?

A server is simply "something" that stores assets and, provides those assets when requested (usually by your browser). An example of this is when you open your browser and, you visit your favorite website. Let's say your favorite website is https://www.mountain-forecast.com/. When you type in the URL, or web address, of your favorite website into your search bar and press enter, you are sending an HTTP request to a server. That server receives your request and sends you back the appropriate assets. These assets are usually a collection of files (HTML documents, images, CSS stylesheets and, JavaScript files).

In the previous paragraph, I used some terminology that should be defined in more detail...

I mentioned that a server is simply "something"... More specifically, a server is either software, hardware or, a combination of both working together. The hardware is usually a computer that physically stores the assets to be served, in memory. The software is usually a system that describes how web users (the clients) are able to access the stored assets.

HTTP is an abbreviation that stand for Hypertext Transfer Protocol. HTTP is essentially a procedure or system of rules for how data is transferred on the World Wide Web. Any sort of data can be transferred as long as both ends can read the data. Some important points regarding HTTP are that, HTTP is connectionless. This means that after making a request and receiving a response, the client and the server are no longer connected. Next, HTTP is stateless. This means that the client and the server only know about each other during the current request. Once the request is fulfilled, they no longer know about each other until another request is made. Finally, there is something called the Transport Layer. The Transport Layer is a collection of methods or transport protocols, known as the internet protocol suite, that simply defines how data is sent from one place to another. More specifically, this suite of protocols defines how the data is formatted, addressed, transmitted, routed and received. The best-known transport protocol is the Transmission Control Protocol (TCP).

Now that we know a little about the client-server model and HTTP, it's time to start looking as some code!

Creating a server with Node

To create a server with NodeJS we will be using the built-in Node module http. This module allows Node to transfer data over the Hypertext Transfer Protocol.

const http = require("http");

// Localhost
const hostname = "127.0.0.1";
const port = 3000;

const server = http.createServer((req, res) => {

    const html = `
        <h1>Hello World!</h1>
        <p>We created our simple server!</p>
    `;

    res.statusCode = 200;
    res.setHeader("Content-Type", "text/html");
    res.write(html);
    res.end();
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});
Enter fullscreen mode Exit fullscreen mode

We first start by defining a hostname and port. The hostname, or IP address, 127.0.0.1 is simply localhost. In principle, this means that you are communicating with your own computer. The port is a communication endpoint. You can have multiple web servers running on your machine by using different ports for the same IP address.

Next we create our server with the createServer() method. This method takes a callback function as a parameter. This callback function receives 2 parameters itself, a request and response object. The request object contains information about the request coming from the client. The response is what is sent back to the client.

In this example we simply set an HTTP status code of 200 (OK), set the HTTP headers to "text/html" so that the client knows we are responding with HTML code, write HTML code within the response and, end the response.

Finally, we start our server with the listen() method. This method takes a port and host as parameters to know where to start the server. This method also takes a callback function as a parameter that simply runs after the server is started. In this example we console log information about the server upon start.

We have successfully created a Node server without Express!
Alt Text

Conclusion

I hope this gives a good starting point for understanding how web servers work and, how the client-server model works. The client-server model is simply a system design for communicating data across the World Wide Web. It uses HTTP to transfer data and, relies on a client and server to exist on either end. This article is meant to give a very basic introduction but, I encourage you to dive deeper into this technology by conducting further research. There is a lot of interesting information to learn within this topic!


Contact

If you have any questions or comments please feel free to reach out!

Top comments (0)

Create an Account!
Now it's your turn!
 
🗒 Share a tutorial
🤔 Reflect on your coding journey
❓ Ask a question

Create an account to join hundreds of thousands of DEV members on their journey.