Fork me on GitHub

diet 0.9

Get Started

level:
duration:
5 minutes

Table of Contents

# Intro

Hey there! Thanks for trying out diet! If you follow these guides you'll be shortly a node ninja! But before you dive in, make sure you understand the syntax of javascript, callbacks and closures.

# Install Node.js

First things first! Diet is a module for node.js, so obviously you need node before diet. If you already have it that's great, just skip to the next section! If you don't, just spend 30 more seconds to install NVM and Node.js on your machine:

# Install the Node Version Manager
curl "https://raw.githubusercontent.com/creationix/nvm/v0.19.0/install.sh" | bash
echo "source ~/.nvm/nvm.sh" >> ~/.bashrc
echo "source ~/.nvm/nvm.sh" >> ~/.profile
echo "source ~/.nvm/nvm.sh" >> ~/.zshrc
source ~/.nvm/nvm.sh

# Install Node.js
nvm install 0.10.33

# Set default node.js version
nvm alias default 0.10.33

# Project Structure

If you have all the above then you can start thinking about writing your code. However before you write any code it's good to know how to structure it. Usually people don't just write code anywhere.


We use Projects. Projects aim to solve something so we call them applications or apps. Each project has a main folder that contains a bunch of files and folders. Diet projects usually start with an index.js file.

# A sample project structure for diet.js

/yourProject
--- /node_modules          # Node Modules
--- /routes                # Routes 
--- /models                # Models (usually database models)
--- /controllers           # Controllers (functions)
--- /views                 # Views (static files, templates etc.)
------ /html               # HTML Templates
------ /scripts            # Javascripts (client-side)
------ /styles             # CSS (Stylesheets)       
------ /images             # Images
--- index.js               # Index (your server starts here)
--- config.json            # Environment Specific Variables (local/dev/production)
--- package.json           # Project Description & Dependencies

If you want to generate a basic diet project with the above structure you can use our Yo generator:

# install yo and generator-diet
npm install -g yo
npm install -g generator-diet

# genereate new diet project
yo diet

# Install Diet

It's a good practice to install a copy of diet for every project instead of installing diet globally to avoid version related problems. Navigate to your project and install diet with NPM:

# navigate to your project
cd /yourProject

# install diet
npm install diet

# Hello World!

It's time to say hello to the world! Save this snippet of code in your index.js:


    // Create an app
    var server = require('diet')
    var app = server()
    app.listen('http://localhost:8000')
    
    // When http://localhost:8000/ is requested, respond with "Hello World!"
    app.get('/', function($){
        $.end('Hello World!')
    })
	

You can start your app from the terminal with:


    node index.js
	

You need to restart your app every time you edit your code. If you are like me and you don't like this then checkout nodemon. After you installed nodemon you can start your app by simply entering "nodemon" instead of "node index.js" and your app will restart every time you edit it.

# What's next?

Congratulations, you're one step closer to world domination! Now that you know how to setup your project and you've seen a working hello world app, it's time to understand how diet works.

You can continue by learning to configure hosts.