I already made a post about what Express really is and which problem Express tries to solve. But do you think about what existed before Express? How did developers build servers in the JavaScript ecosystem?
I had this question, and I found out a really interesting thing.
In the JavaScript ecosystem, Node.js was released in 2009, and its standard library already allowed developers to create HTTP servers directly.
Think about the complexity and repetition of code for building servers using the Node.js HTTP module. I saw code for handling different routes, where you handle different types of requests and routes using the if-else block.
Like this:
const server = http.createServer((req, res) => {
if (req.method === "GET" && req.url === "/") {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Home page");
} else if (req.method === "GET" && req.url === "/about") {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("About page");
} else {
res.writeHead(404, { "Content-Type": "text/plain" });
res.end("404 - Page not found");
}
});
You repetitively write which data type you send and what the actual data (response) will be sent by the server. And this process becomes cumbersome as the application grows.
Developers therefore built higher-level abstractions around Node.js HTTP capabilities, which are frameworks and libraries. Express is one of those abstractions and became particularly influential.
Node.js brought JavaScript to the server, and Express provided a popular web framework abstraction for that environment.
Top comments (0)