-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new server examples -- blog and upload
- Loading branch information
Showing
10 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<html> | ||
<head> | ||
<title>Upload</title> | ||
</head> | ||
<body> | ||
<form method="post" action="/upload" enctype="multipart/form-data"> | ||
<input type="file" name="file"> | ||
<input type="submit" name="submit" value="submit"> | ||
</form> | ||
</body> | ||
</html> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
Adapted from: | ||
https://github.com/antiboredom/servi-ide/tree/master/examples/file-uploads | ||
*/ | ||
var servi = require('servi'); | ||
var app = new servi(true); | ||
port(3003); | ||
start(); | ||
|
||
serveFiles('uploads'); | ||
|
||
route('/', form); | ||
function form(request){ | ||
request.serveFile('form.html'); | ||
} | ||
|
||
route('/upload', upload); | ||
function upload(request) { | ||
var file = request.files.file; | ||
uploadFile(file, "uploads"); | ||
request.respond('<img src="' + file.name + '">'); | ||
} |
49 changes: 49 additions & 0 deletions
49
10-server-side/12-servi-blog-templates-db-uploads/public/style.css
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
body { | ||
font: 16px sans-serif; | ||
background-color: #eee; | ||
width: 600px; | ||
margin: auto; | ||
line-height: 1.4; | ||
} | ||
|
||
a { | ||
color: #ff0046; | ||
text-decoration: none; | ||
} | ||
|
||
h2 { | ||
padding:0; | ||
margin:0; | ||
} | ||
|
||
.entry { | ||
border-bottom: 1px dashed #ccc; | ||
padding: 20px 0; | ||
} | ||
|
||
.entry img { | ||
max-width: 600px; | ||
margin-bottom: 10px; | ||
} | ||
|
||
textarea, input { | ||
display: block; | ||
width: 100%; | ||
font: 16px sans-serif; | ||
padding: 5px; | ||
border: 1px solid #ccc; | ||
margin-bottom: 10px; | ||
} | ||
|
||
textarea { | ||
height: 200px; | ||
} | ||
|
||
input[type=submit] { | ||
background-color: 0; | ||
color: #fff; | ||
} | ||
|
||
input[type=file] { | ||
border: 0; | ||
} |
52 changes: 52 additions & 0 deletions
52
10-server-side/12-servi-blog-templates-db-uploads/server.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
Adapted from: | ||
https://github.com/antiboredom/servi-ide/tree/master/examples/blog | ||
*/ | ||
var servi = require('servi'); | ||
var app = new servi(true); | ||
port(3002); | ||
start(); | ||
|
||
var database = useDatabase('blog'); | ||
serveFiles('public'); | ||
|
||
route('/', home); | ||
route('/new', newEntry); | ||
route('/entries/:id', showEntry); | ||
|
||
function home(request) { | ||
var data = database.find({}); | ||
data.sort({ date: -1 }); | ||
data.exec(function(err, entries){ | ||
request.render('templates/home.html', {entries: entries}); | ||
}); | ||
} | ||
|
||
function newEntry(request) { | ||
if (request.method == GET) { | ||
request.render('templates/form.html'); | ||
} | ||
|
||
else if (request.method == POST) { | ||
var image = request.files.image; | ||
uploadFile(image, "public/uploads"); | ||
|
||
var blogEntry = { | ||
content: request.fields.content, | ||
title: request.fields.title, | ||
date: new Date(), | ||
image: image.name | ||
} | ||
database.insert(blogEntry); | ||
request.redirect('/'); | ||
} | ||
} | ||
|
||
function showEntry(request) { | ||
var id = request.params.id; | ||
database.findOne({'_id': id}, function(err, theEntry){ | ||
request.render('templates/blogpost.html', {entry: theEntry}); | ||
}); | ||
} |
17 changes: 17 additions & 0 deletions
17
10-server-side/12-servi-blog-templates-db-uploads/templates/.tmpscript
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
var servi = require('/Users/sam/Dropbox/school/spring_2014/servi/app/servi.js');var app = new servi(); | ||
|
||
|
||
<% include head.html %> | ||
<% posts.forEach(function(post){ %> | ||
<div class="post"> | ||
<h2><a href="/posts/<%= post._id %>"><%= post.title %></a></h2> | ||
<div class="post-content"> | ||
<%= post.content %> | ||
</div> | ||
</div> | ||
<% }) %> | ||
<% include foot.html %> | ||
|
||
|
||
|
||
if (typeof run === 'function') app.defaultRoute(run);start(); |
11 changes: 11 additions & 0 deletions
11
10-server-side/12-servi-blog-templates-db-uploads/templates/blogpost.html
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<% include head.html %> | ||
<div class="entry"> | ||
<h2><a href="/entries/<%= entry._id %>"><%= entry.title %></a></h2> | ||
<% if (entry.image) { %> | ||
<img src="/uploads/<%= entry.image %>"> | ||
<% } %> | ||
<div class="entry-content"> | ||
<%= entry.content %> | ||
</div> | ||
</div> | ||
<% include foot.html %> |
2 changes: 2 additions & 0 deletions
2
10-server-side/12-servi-blog-templates-db-uploads/templates/foot.html
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
</body> | ||
</html> |
11 changes: 11 additions & 0 deletions
11
10-server-side/12-servi-blog-templates-db-uploads/templates/form.html
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<% include head.html %> | ||
<form method="post" action="/new" enctype="multipart/form-data"> | ||
<label for="title">Title</label> | ||
<input type="text" name="title"> | ||
<label for="image">Image</label> | ||
<input type="file" name="image"> | ||
<label for="content">Content</label> | ||
<textarea name="content"></textarea> | ||
<input type="submit" value="Post"> | ||
</form> | ||
<% include foot.html %> |
8 changes: 8 additions & 0 deletions
8
10-server-side/12-servi-blog-templates-db-uploads/templates/head.html
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<html> | ||
<head> | ||
<link rel="stylesheet" href="/style.css" type="text/css" /> | ||
<title>My Blog</title> | ||
</head> | ||
<body> | ||
<h1>My Blog</h1> | ||
<p><a href="/new">New Entry</a></p> |
13 changes: 13 additions & 0 deletions
13
10-server-side/12-servi-blog-templates-db-uploads/templates/home.html
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<% include head.html %> | ||
<% entries.forEach(function(entry){ %> | ||
<div class="entry"> | ||
<h2><a href="/entries/<%= entry._id %>"><%= entry.title %></a></h2> | ||
<% if (entry.image) { %> | ||
<img src="/uploads/<%= entry.image %>"> | ||
<% } %> | ||
<div class="entry-content"> | ||
<%= entry.content %> | ||
</div> | ||
</div> | ||
<% }) %> | ||
<% include foot.html %> |