nickel.rs

web application framework for rust

Getting Started

Getting started with nickel.rs is extremely easy. This guide will walk you through the steps necessary to write your first nickel app.

1. Get a recent version of Rust and Cargo

In order to write a nickel app you first need to make sure to have a recent version of Rust and Cargo. In case you are wondering Cargo is the package manager for Rust. It makes dependency and build management a breeze. The easiest way to install both is just a simple one liner on your shell.

$ curl -s https://static.rust-lang.org/rustup.sh | sudo sh

2. Let Cargo generate a new project directory and cd into it

$ cargo new nickel-demo --bin && cd nickel-demo
By feeding --bin to the cargo new we command cargo to choose the project folder layout for programs instead of libraries. This will also make sure that we can later just use cargo run to start the program.

3. Edit the generated Cargo.toml file

The Cargo.toml file is the manifest that describes all dependencies of your app and also tells Cargo how to build the project. Just append the following to the file to make Cargo aware of the nickel.rs dependency.


[dependencies]
nickel = "*"
By now your Cargo.toml file should look like this:

[package]

name = "my-demo"
version = "0.0.1"
authors = ["Your Name <your.name@somewhere.com>"]


[dependencies]
nickel = "*"

4. Edit the generated main.rs file

Cargo generated a main.rs in the src directory of your project. It comes predefined with a hello world implementation. This is where we will put the code for your first nickel app. Delete the content and overwrite it with this:


#[macro_use] extern crate nickel;

use nickel::Nickel;

fn main() {
    let mut server = Nickel::new();

    server.utilize(router! {
        get "**" => |_req, _res| {
            "Hello world!"
        }
    });

    server.listen("127.0.0.1:6767").unwrap();
}

5. Run your app

Congrats! You created your first nickel app. Just type cargo run on your console and visit http://localhost:6767 in your browser. It should print out hello world.