Created
August 15, 2014 21:21
-
-
Save jpadilla/07ef8d740da29393ecc5 to your computer and use it in GitHub Desktop.
Simple examples for Building for the Web: Day 2 - The Backend
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var express = require('express'); | |
var app = express(); | |
app.get('/api/v1/songs', function(req, res) { | |
var data = { | |
songs: [{ | |
id: 1, | |
album: 1, | |
favorite: false, | |
name: 'Pretty When You Cry' | |
}, { | |
id: 1, | |
album: 1, | |
favorite: false, | |
name: 'Money Power Glory' | |
}] | |
}; | |
res.json(data); | |
}); | |
var server = app.listen(8000, function() { | |
console.log('Listening on port %d', server.address().port); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, jsonify | |
app = Flask(__name__) | |
@app.route('/api/v1/songs') | |
def songs(): | |
data = { | |
'songs': [{ | |
'id': 1, | |
'album': 1, | |
'favorite': False, | |
'name': 'Pretty When You Cry' | |
}, { | |
'id': 2, | |
'album': 1, | |
'favorite': True, | |
'name': 'Money Power Glory' | |
}] | |
} | |
return jsonify(data) | |
if __name__ == '__main__': | |
app.run() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
require 'json' | |
get '/api/v1/songs' do | |
data = { | |
'songs' => [{ | |
'id' => 1, | |
'album' => 1, | |
'favorite' => false, | |
'name' => 'Pretty When You Cry' | |
}, { | |
'id' => 2, | |
'album' => 1, | |
'favorite' => true, | |
'name' => 'Money Power Glory' | |
}] | |
} | |
data.to_json | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"log" | |
"net/http" | |
"os" | |
) | |
type Response map[string]interface{} | |
func (r Response) String() (s string) { | |
b, err := json.Marshal(r) | |
if err != nil { | |
s = "" | |
return | |
} | |
s = string(b) | |
return | |
} | |
type Song struct { | |
Id int `json:"id"` | |
Album int `json:"album"` | |
Name string `json:"name"` | |
Favorite bool `json:"favorite"` | |
} | |
func songsHandler(w http.ResponseWriter, r *http.Request) { | |
if r.Method == "GET" { | |
songs := make([]Song, 2) | |
songs[0] = Song{ | |
Id: 1, | |
Name: "Pretty When You Cry", | |
Album: 1, | |
Favorite: false, | |
} | |
songs[1] = Song{ | |
Id: 2, | |
Name: "Money Power Glory", | |
Album: 1, | |
Favorite: true, | |
} | |
data := Response{"songs": songs} | |
w.Header().Set("Content-Type", "application/json") | |
fmt.Fprint(w, data) | |
return | |
} | |
} | |
func main() { | |
http.HandleFunc("/api/v1/songs", songsHandler) | |
err := http.ListenAndServe(getPort(), nil) | |
if err != nil { | |
log.Fatal("ListenAndServe: ", err) | |
return | |
} | |
} | |
func getPort() string { | |
var port = os.Getenv("PORT") | |
// Set a default port if there is nothing in the environment | |
if port == "" { | |
port = "8080" | |
} | |
log.Println("INFO: Listening to http://localhost:" + port) | |
return ":" + port | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment