C# Minimal API - the node js migration
Minimal API is a feature introduced in .NET 6 that simplifies the process of building APIs. This feature is designed to help developers write less code, reduce boilerplate code, and increase productivity. Minimal API provides a lightweight, streamlined syntax for creating APIs that are easy to understand, maintain, and extend.
In this article, I will explore the concept of minimal API in C# and look at how a similar approach is done in node js.
What is Minimal API in C#?
Minimal API is a way of building APIs that are designed to be lightweight and easy to use. Minimal API uses a feature introduced in C# 9 called top-level statements, which allows developers to write code without the need for a class or method.
Minimal API is based on the concept of convention over configuration, which means that developers can write less code by following a set of conventions. This approach reduces the need for boilerplate code, and it helps developers to focus on writing the core functionality of their APIs.
It also provides a simplified routing syntax that allows mapping endpoints to methods without the need for attributes or controllers. This approach makes it easy to build APIs without having to worry about the details of the routing system.
How to use Minimal API?
To use Minimal API, you need to install at least .NET 6 and create a new project. You can do this using the following steps:
Install .NET 6: To install .NET 6, go to the official .NET website and download the latest version of the .NET SDK.
Create a new project: Once you have installed .NET 6, open the command prompt or terminal and run the following command:
dotnet new web -o minapi
This command creates a new web project named "minapi" in the current directory.
Add a minimal API endpoint: Now, open the
Program.cs
file in the minapi project and replace the code with the following:
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
WebApplication app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.Run("http://localhost:5000");
In this code, we are using the WebApplication.CreateBuilder
method to create a new instance of the WebApplicationBuilder
class. We call the Build
method to create an instance of WebApplication
Then we use the MapGet
method to map a GET request to the root endpoint ("/") to a lambda expression that returns the string "Hello World!
".
Finally, we use the Run
method to start the web server and listen for incoming requests.
Run the application: To run the application, open the command prompt or terminal and navigate to the "minapi" directory. Then run the following command:
dotnet run
This command will start the web server and listen for incoming requests.
Test the endpoint: Now, open a web browser and navigate to http://localhost:5000/. You should see the message "Hello World!" displayed in the browser.
While minimal APIs have only been around since .Net 6, one would argue that this is driven by what has already been implemented in other languages and frameworks. Let's consider how a web server is set up in node js.
const http = require('http');
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(3000, '127.0.0.1', () => {
console.log(`Server running at http://127.0.0.1:3000/`);
});
This server creates a basic HTTP server using the built-in Node.js http
module. It listens on port 3000
of the localhost
IP address (127.0.0.1
). When a client makes a request to the server, the server responds with a simple Hello World
message.
To run this server, save the code in a file with a .js
extension (e.g. server.js
), navigate to the file's directory in your terminal or command prompt, and run the command node server.js
. This will start the server and print Server running at
http://127.0.0.1:3000/
to the console. You can then open a web browser and visit http://localhost:3000/
to see the Hello World
message. Note that you can only run this code if you have node installed on your machine.
A lot of developers have always liked how simple creating a server in node js is. With only a few lines of code, you have your node js server up and running. In C#, it has required a lot of boilerplate code and loads of pipeline configuration. Minimal API has changed all this!
Conclusion
Minimal API is a feature introduced in .NET 6 that simplifies the process of building APIs. It is designed to help developers write less code, reduce boilerplate code, and increase productivity. It provides a lightweight, streamlined syntax for creating APIs that are easy to understand, maintain, and extend.
In this article, I explored the concept of minimal API and looked at some examples to understand how it works. I also discussed how to use minimal API by creating a new project and adding a minimal API endpoint. C# developers now have a node js equivalent in terms of the simplicity of setting up a web service.
Subscribe to my newsletter
Read articles from Ronald Kainda directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Ronald Kainda
Ronald Kainda
I am a passionate software engineer driven by a deep fascination with how technology can elegantly solve real-world problems. With a strong belief in the power of innovation, I thrive on creating cutting-edge solutions that make a meaningful impact on people's lives and the world around us. My dedication to excellence and continuous learning enables me to stay at the forefront of technological advancements, always seeking to leverage the latest tools and frameworks to deliver robust and scalable software solutions. I take pride in crafting efficient and user-centric applications that not only meet the needs of today but also anticipate the challenges of tomorrow. Beyond my technical expertise, I have a keen interest in venture capital and startup ecosystems. I am captivated by the dynamic and transformative nature of entrepreneurship. My desire to understand the business side of technology and my analytical mindset fuel my enthusiasm for exploring innovative opportunities and identifying high-potential ventures. As a software engineer, I embrace collaboration, seeing every project as an opportunity to work alongside talented teams and foster an environment of creativity and growth. I am motivated by the prospect of being part of ventures that drive positive change and shape a better future. In essence, my personal brand stands for a software engineer who is not only passionate about the intricacies of coding but also deeply motivated by the potential of technology to create meaningful solutions and the captivating world of venture capital. Unless explicitly stated, the opinions expressed on this blog are mine and do not represent that of any organisation I am associated with.