The default hello world app

In the terminal window, go to your folder of choice or create a new one, for example c:\vibeprojects. Inside that folder, type the command

dub init hello --type=vibe.d

or

dub init hello -t vibe.d

And press Enter at every prompt that follows to accept the defaults.

c:\vibeprojects>dub init hello -t vibe.d
Package recipe format (sdl/json) [json]:
Name [hello]:
Description [A simple vibe.d server application.]:
Author name [Owner]:
License [proprietary]:
Copyright string [Copyright © 2023, Owner]:
Add dependency (leave empty to skip) []:
     Success created empty project in c:\vibeprojects\hello
             Package successfully created in hello

Go inside the newly created hello project folder and explore it.

c:\vibeprojects>cd hello
c:\vibeprojects\hello>dir
 Volume in drive C has no label.
 Volume Serial Number is 4E0A-BFEF
 Directory of c:\vibeprojects\hello
2023-01-04  01:30 PM    <DIR>          .
2023-01-04  01:30 PM    <DIR>          ..
2023-01-04  01:30 PM               131 .gitignore
2023-01-04  01:30 PM               215 dub.json
2023-01-04  01:30 PM    <DIR>          public
2023-01-04  01:30 PM    <DIR>          source
2023-01-04  01:30 PM    <DIR>          views
               2 File(s)            346 bytes
               5 Dir(s)  134,991,044,608 bytes free

You are now inside the root directory of the hello application. To compile and run this application in one step (you haven’t done anything yet!), type

dub

Since this is the first time you will be running this command, this will download all the dependencies, then compile, then link, and then run the application.

c:\vibeprojects\hello>dub

There will be a lot of messages before you see this at the tail end:

     Linking hello
             Copying files for vibe-d:tls...
     Running hello.exe
[main(----) INF] Listening for requests on http://[::1]:8080/
[main(----) INF] Listening for requests on http://127.0.0.1:8080/
[main(----) INF] Please open http://127.0.0.1:8080/ in your browser.

The last three lines indicate that the application has successfully compiled and the server is now running.

Open your browser and point it to localhost, port 8080.

http://127.0.0.1:8080

or

http://localhost:8080

and you will see the ‘Hello, World!’ message in your browser.

To stop the running application, press Ctrl-C (Control-C) on the terminal window. Sometimes you have to do it twice.

[00000000(----) INF] Received signal 2. Shutting down.
[main(----) INF] Stopped to listen for HTTP requests on ::1:8080
[main(----) INF] Stopped to listen for HTTP requests on 127.0.0.1:8080
^C
c:\vibeprojects\hello>

The common command options for dub are

dub init - create a new project

dub run - build and run combined

dub - same as dub run

dub build - completely compile the whole project and its dependencies but don’t run

dub test - runs the unit tests

Let us take a closer look at the default application.

Open VS Code and go to File->Open folder…

Then open the new application folder hello.

In the Explorer window of VS Code, open source\app.d and you will see the automatically generated code.

import vibe.vibe;
void main()
{
  auto settings = new HTTPServerSettings;
  settings.port = 8080;
  settings.bindAddresses = ["::1", "127.0.0.1"];
  auto listener = listenHTTP(settings, &hello);
  scope (exit)
  {
    listener.stopListening();
  }
  logInfo("Please open http://127.0.0.1:8080/ in your browser.");
  runApplication();
}
void hello(HTTPServerRequest req, HTTPServerResponse res)
{
  res.writeBody("Hello, World!");
}

In the main() method of source\app.d:

  • a new HTTPServerSettings object is created

  • the HTTP port number is set to 8080 (which you can change)

  • the IP the server listens to is set to localhost in both IPv6 (“::1”) and IPv4 (“127.0.0.1”) format

  • the listenHTTP(settings, &hello) call means ‘run the hello() function using these settings’

  • the runApplication() call starts the ball rolling (starts the event loop)

Last updated