First steps
Get started with Cargo
A Cargo application can be easily created with just a few lines of code. Cargo tries to scale with your needs. So if you have a really small application you can put everthing in just 1 file and your are good to go. For more complex applications Cargo provides a set of useful helper components and some guidelines as recommendation. But more on that later!
Lets get our hands coding
In this example you will learn the very basic core fundamentals of a Cargo application. This includes the follwing basic building block:
- Register a route and a handler function in your application.
- Bootstrap the application
- Run the application listening with the default http protocol on http://localhost:8000.
Requirements
Make sure to install a current version of Deno before you continue with with the next steps. You can download and install Deno from the official website
1. Create project folder and an application file
- Create a new project folder. For examples:
cargo-getting-started
- Change into the new folder
- Create new application file called
app.ts
2. Register a route and bootstrap the application
Open the created file app.ts
in a code editor of your choice and add the follwing lines of code:
import { bootstrap } from "https://deno.land/x/cargo/mod.ts";
import { Get } from "https://deno.land/x/cargo/http/mod.ts";
Get("/",() => {
return new Response("Hello World!")
});
(await bootstrap()).run();
Yes, thats all you need to run your first Cargo application.
3. Run the application
Now its time to look at the result of our code above. Do the following steps to run the application:
- Open your terminal
- Run the Cargo application with the command:
deno run --allow-net app.ts
- Open your browser and visit the following domain: http://localhost:8000
Congratulation! You just got succesfully your first Cargo application up and running.