# 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:

1. Install .NET 6: To install .NET 6, go to the official .NET website and download the latest version of the .NET SDK.
    
2. 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.
    
3. Add a minimal API endpoint: Now, open the `Program.cs` file in the minapi project and replace the code with the following:
    

```csharp

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.

1. 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.
    
2. Test the endpoint: Now, open a web browser and navigate to [**http://localhost:**](http://localhost:5000/)**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.

```javascript
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`](http://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/`](http://127.0.0.1:3000/) to the console. You can then open a web browser and visit [`http://localhost:3000/`](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.
