Permalink
Browse files

switch from old connect to newer express

  • Loading branch information...
1 parent d355fa8 commit c8b9c92ea99cebf0a297393b0eb665d576a87baf @astro astro committed Oct 24, 2013
Showing with 88 additions and 107 deletions.
  1. +1 −1 package.json
  2. +87 −106 server.js
View
@@ -8,7 +8,7 @@
, "engines": { "node": "*" }
, "dependencies": {
"formidable": ">= 0.9.11",
- "connect": ">= 0.5.10",
+ "express": ">= 3.4.3",
"ndns": ">= 0.1.1"
}
}
View
193 server.js
@@ -1,127 +1,108 @@
-var connect = require('connect');
+var express = require('express');
var formidable = require('formidable');
var files = require('./files');
var template = require('./template');
var sys = require('sys');
var MAX_FILE_SIZE = 300 * 1024 * 1024;
+var HTML_HEADERS = { 'Content-Type': 'text/html; charset=UTF-8' };
-function enforceVhost(vhost) {
- return function(req, res, next) {
- if (!req.headers['host'] || req.headers['host'] === vhost)
- return next();
+var app = express();
- res.writeHead(302, { 'Location': 'http://' + vhost + '/' });
- res.end();
- };
-}
-
-function app(app) {
- var HTML_HEADERS = { 'Content-Type': 'text/html; charset=UTF-8' };
- app.get('/', function(req, res) {
- res.writeHead(200, HTML_HEADERS);
- res.write(template.htmlHead);
- res.write(template.uploadForm);
- res.write(template.downloadList(files.getFiles()));
- res.write(template.htmlFoot);
- res.end();
- });
+app.get('/', function(req, res) {
+ res.writeHead(200, HTML_HEADERS);
+ res.write(template.htmlHead);
+ res.write(template.uploadForm);
+ res.write(template.downloadList(files.getFiles()));
+ res.write(template.htmlFoot);
+ res.end();
+});
- app.post('/upload', function(req, res) {
- var out, error;
+app.post('/upload', function(req, res) {
+ var out, error;
- var form = new formidable.IncomingForm();
- form.encoding = 'utf8';
- form.handlePart = function(part) {
- if (part.name === 'file' && part.filename) {
- console.log({ part: { name: part.name,
- filename: part.filename,
- mime: part.mime
- } });
- out = files.addFile({ name: part.filename,
- type: part.mime
- });
- var received = 0;
- part.on('data', function(data) {
- if (out) {
- received += data.length;
- if (received > MAX_FILE_SIZE) {
- out.discard();
- out = undefined;
+ var form = new formidable.IncomingForm();
+ form.encoding = 'utf8';
+ form.handlePart = function(part) {
+ if (part.name === 'file' && part.filename) {
+ console.log({ part: { name: part.name,
+ filename: part.filename,
+ mime: part.mime
+ } });
+ out = files.addFile({ name: part.filename,
+ type: part.mime
+ });
+ var received = 0;
+ part.on('data', function(data) {
+ if (out) {
+ received += data.length;
+ if (received > MAX_FILE_SIZE) {
+ out.discard();
+ out = undefined;
- files.freeBytes(received);
- error = new Error('Exceeded maximum file size');
- /* TODO: send response ASAP */
- } else {
- files.allocBytes(data.length);
- req.pause();
- out.write(data, function() {
- req.resume();
- });
- }
- }
- });
- part.on('end', function() {
- if (out && received > 0) {
- out.commit(received);
- } else if (out) {
- out.discard();
- error = new Error('File is empty');
- }
- });
- }
- };
- form.parse(req, function(err, fields, files) { if (!error) error = err; });
+ files.freeBytes(received);
+ error = new Error('Exceeded maximum file size');
+ /* TODO: send response ASAP */
+ } else {
+ files.allocBytes(data.length);
+ req.pause();
+ out.write(data, function() {
+ req.resume();
+ });
+ }
+ }
+ });
+ part.on('end', function() {
+ if (out && received > 0) {
+ out.commit(received);
+ } else if (out) {
+ out.discard();
+ error = new Error('File is empty');
+ }
+ });
+ }
+ };
+ form.parse(req, function(err, fields, files) { if (!error) error = err; });
- form.on('end', function() {
- if (!out && !error)
- error = new Error('No file received');
+ form.on('end', function() {
+ if (!out && !error)
+ error = new Error('No file received');
- if (error) {
- /* Error */
- console.error(error.stack);
- res.writeHead(400, HTML_HEADERS);
- res.write(template.htmlHead);
- res.write(template.error(error || new Error('No file received')));
- res.write(template.htmlFoot);
- res.end();
- } else {
- /* Success */
- res.writeHead(200, HTML_HEADERS);
- res.write(template.htmlHead);
- res.write(template.thanks);
- res.write(template.htmlFoot);
- res.end();
- }
- });
+ if (error) {
+ /* Error */
+ console.error(error.stack);
+ res.writeHead(400, HTML_HEADERS);
+ res.write(template.htmlHead);
+ res.write(template.error(error || new Error('No file received')));
+ res.write(template.htmlFoot);
+ res.end();
+ } else {
+ /* Success */
+ res.writeHead(200, HTML_HEADERS);
+ res.write(template.htmlHead);
+ res.write(template.thanks);
+ res.write(template.htmlFoot);
+ res.end();
+ }
});
-}
+});
function fileDownload(req, res, next) {
var m;
if ((m = req.url.match(/^\/file\/(\d+)\//))) {
- var id = parseInt(m[1], 10);
- var info = files.getFile(id);
- res.writeHead(200, { 'Content-Type': info.mime,
- 'Content-Length': info.size });
- var read = files.readFile(info.id);
- sys.pump(read, res);
- read.on('close', function() {
- res.end();
- });
+ var id = parseInt(m[1], 10);
+ var info = files.getFile(id);
+ res.writeHead(200, { 'Content-Type': info.mime,
+ 'Content-Length': info.size });
+ var read = files.readFile(info.id);
+ sys.pump(read, res);
+ read.on('close', function() {
+ res.end();
+ });
} else
- next();
+ next();
}
-var optionalVhostEnforce = process.env.VHOST ?
- enforceVhost(process.env.VHOST) :
- function(req, res, next) { return next(); };
-
-connect.createServer(
- connect.logger(),
- optionalVhostEnforce,
- connect.router(app),
- fileDownload,
- connect.static(__dirname + '/public'),
- connect.errorHandler({ dumpExceptions: true, showStack: true })
-).listen(parseInt(process.env.PORT || "8000", 10), '::');
+app.use(fileDownload);
+app.use(express.static(__dirname + '/public'));
+app.listen(parseInt(process.env.PORT || "8000", 10), '::');

0 comments on commit c8b9c92

Please sign in to comment.