Skip to main content

Command Palette

Search for a command to run...

Dart Backend for Node.js Developers

Updated
3 min readView as Markdown
Dart Backend for Node.js Developers
J

Jamiu is a Flutter Google Developer Expert (GDE) and Mobile Engineer with over 6 years of experience building high-quality mobile apps. He founded the FlutterBytes Community to empower developers and actively contribute to the Flutter ecosystem worldwide.

Dart can be used for backend development just like Node.js, leveraging its built-in dart:io library. If you're a Node.js developer, you'll find many similarities in how Dart handles HTTP servers, with some key differences in syntax and paradigms.

Disclaimer: This article focuses on using Dart's built-in dart:io library for HTTP servers, similar to Node.js' built-in http module. A discussion on frameworks like Express.js (Node.js) and Shelf (Dart) will be covered in a separate resource.


Prerequisites

Before diving in, ensure you have the following:

  • Basic knowledge of Dart and Node.js.

  • Dart SDK installed.

  • A code editor like VS Code or IntelliJ IDEA.

  • A terminal to run Dart scripts.

  • Postman (or a browser) for testing API requests.


1. Setting Up Your First Dart HTTP Server

Step 1: Create a New Dart File

Create a new file server.dart in your working directory.

Step 2: Add the Following Code

Open server.dart and add this basic HTTP server implementation:

import 'dart:io';

void main() async {
  final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 3000);
  print('Server running on http://${server.address.address}:${server.port}');

  server.listen((HttpRequest request) {
    request.response.statusCode = HttpStatus.ok;
    request.response.headers.contentType = ContentType.html;
    request.response.write('<h1>Hello, World!</h1>');
    request.response.close();
  });
}

Step 3: Running the Server

Run the following command in your terminal:

dart server.dart

(This is similar to running node app.js in Node.js.)

Step 4: Testing the Server

Open your browser and go to http://localhost:3000/ or use Postman to send a GET request.

Step 5: Using the Cascading Approach (Shortened Code)

We can rewrite the response handling more concisely using the cascade notation:

server.listen((HttpRequest request) {
  request.response
    ..statusCode = HttpStatus.ok
    ..headers.contentType = ContentType.html
    ..write('<h1>Hello, World!</h1>')
    ..close();
});

2. Handling Routes and HTTP Methods

Let's extend our server to handle multiple routes and HTTP methods.

Handling Different Routes and Methods

void main() async {
  final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 3000);
  print('Server running on http://${server.address.address}:${server.port}');

  server.listen((HttpRequest request) {
    if (request.uri.path == '/' && request.method == 'GET') {
      request.response
        ..headers.contentType = ContentType.html
        ..write('<h1>Welcome to Dart vs Node.js</h1>')
        ..close();
    } else if (request.uri.path == '/data' && request.method == 'POST') {
      request.response
        ..statusCode = HttpStatus.created
        ..write('Data received')
        ..close();
    } else {
      request.response
        ..statusCode = HttpStatus.notFound
        ..write('404 Not Found')
        ..close();
    }
  });
}

3. Assignment

Modify the Dart code to:

  • Add more routes (e.g., /users, /products).

  • Handle more HTTP verbs (PUT, DELETE).

  • Respond with JSON data for certain routes.

Example JSON response:

request.response
  ..headers.contentType = ContentType.json
  ..write('{"message": "User added successfully"}')
  ..close();

4. Next Steps: Exploring Frameworks

While dart:io provides a low-level approach, frameworks like Shelf (Dart) and Express.js (Node.js) simplify development by adding routing, middleware, and request handling.

For those who have experience with Express.js, Dart's Shelf framework uses very similar patterns for routing and middleware.

To see how those skills overlap, consider reading Dart Shelf for Express.js Developers.


Conclusion

  • Dart and Node.js provide built-in HTTP modules, making backend development possible without external dependencies.

  • Dart’s syntax is more structured with await for loops and cascade notation (..), but cascading is optional.

  • Both platforms use event-driven programming, but Dart offers Isolates for concurrency.

  • For complex backends, frameworks like Shelf (Dart) and Express.js (Node.js) can simplify development.

More from this blog

Jamiu Okanlawon

9 posts