Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

redirect

  • Loading branch information...
commit 66e405751edf7835247cebde0ffba8b4323135b8 1 parent ccdce70
Vadim Ogievetsky vogievetsky authored
Showing with 50 additions and 14 deletions.
  1. +27 −7 app.coffee
  2. +21 −5 app.js
  3. +2 −2 public/index.html
34 app.coffee
View
@@ -1,5 +1,5 @@
http = require('http')
-paperboy = require('paperboy')
+send = require('send')
io = require('socket.io')
fs = require('fs')
@@ -43,12 +43,32 @@ setInterval((->
), 2000)
app = http.createServer (req, res) ->
- paperboy
- .deliver(__dirname + '/public', req, res)
- .addHeader('X-PaperRoute', 'Node')
- .before(->
- console.log('Received Request')
- )
+ # your custom error-handling logic:
+ error = (err) ->
+ res.statusCode = err.status or 500
+ res.end(err.message)
+
+ # your custom directory handling logic:
+ redirect = ->
+ res.statusCode = 301;
+ res.setHeader('Location', req.url + '/')
+ res.end('Redirecting to ' + req.url + '/')
+
+ if req.url is '/'
+ res.statusCode = 302
+ res.setHeader('Location', '/dc')
+ res.end('Redirecting to /dc')
+ else if match = req.url.match(/^\/(\w+)$/)
+ send(req, 'index.html')
+ .root('public')
+ .on('error', error)
+ .pipe(res)
+ else
+ send(req, req.url)
+ .root('public')
+ .on('error', error)
+ .pipe(res)
+
return
io = io.listen(app)
26 app.js
View
@@ -1,10 +1,10 @@
// Generated by CoffeeScript 1.3.1
(function() {
- var LOCATION_DIR, app, file, fs, http, io, json, locationName, locationNames, locations, paperboy, _i, _len;
+ var LOCATION_DIR, app, file, fs, http, io, json, locationName, locationNames, locations, send, _i, _len;
http = require('http');
- paperboy = require('paperboy');
+ send = require('send');
io = require('socket.io');
@@ -58,9 +58,25 @@
}), 2000);
app = http.createServer(function(req, res) {
- paperboy.deliver(__dirname + '/public', req, res).addHeader('X-PaperRoute', 'Node').before(function() {
- return console.log('Received Request');
- });
+ var error, match, redirect;
+ error = function(err) {
+ res.statusCode = err.status || 500;
+ return res.end(err.message);
+ };
+ redirect = function() {
+ res.statusCode = 301;
+ res.setHeader('Location', req.url + '/');
+ return res.end('Redirecting to ' + req.url + '/');
+ };
+ if (req.url === '/') {
+ res.statusCode = 302;
+ res.setHeader('Location', '/dc');
+ res.end('Redirecting to /dc');
+ } else if (match = req.url.match(/^\/(\w+)$/)) {
+ send(req, 'index.html').root('public').on('error', error).pipe(res);
+ } else {
+ send(req, req.url).root('public').on('error', error).pipe(res);
+ }
});
io = io.listen(app);
4 public/index.html
View
@@ -2,7 +2,7 @@
<html>
<head>
<title>My Love Letter</title>
- <script src="lib/d3.js"></script>
+ <script src="/lib/d3.js"></script>
<script src="/socket.io/socket.io.js"></script>
<link type="text/css" href="muse.css" rel="Stylesheet" />
@@ -13,5 +13,5 @@
<div class="edit-cont">
</div>
</body>
- <script src="muse.js"></script>
+ <script src="/muse.js"></script>
</html>
Please sign in to comment.
Something went wrong with that request. Please try again.