Skip to content

Commit

Permalink
Merge pull request FineUploader#1 from logickal/node_js_express_example
Browse files Browse the repository at this point in the history
Node.js express example
  • Loading branch information
Ray Nicholus committed Apr 18, 2013
2 parents 32f0846 + e4983ea commit 3822b5e
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 122 deletions.
122 changes: 0 additions & 122 deletions nodejs.js

This file was deleted.

85 changes: 85 additions & 0 deletions nodejs/nodejs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* Node.JS server sample file.
*
*
*
* This code requires external modules, which can be downloaded with the following commands:
* npm install express
* npm install node-uuid
* npm install jade
*
* This code assumes that there is a uploads/ directory for storing the uploaded files, and that the fine-uploader code
* is available at public/fine-uploader/jquery
*
*
* Based in part on Felix Gertz <dev@felixgertz.de> original example.
* Original comments follow:
* Express handles most of the heavy lifting of handling the multipart form parsing - all we have to do is establish an endpoint
* to handle the incoming file
*
* If you are using NginX as reverse proxy, please set this in your server block:
* client_max_body_size 200M;
*
* I don't believe the following is true any longer, as all my testing has been on 8000 - so perhaps needs further validation:
**
** You have to run the server endpoint on port 80,
** either by an reverse proxy upstream to this script
** or by run this script directly on port 80,
** because the ajax upload script can not handle port instruction in the action url correctly. :(
**
*
* @Author: Jeremy Dickens <jeremy@offnominal.com> 2013
*
*/

var express = require('express'),
fs = require('fs'),
util = require('util'),
uuid = require('node-uuid'),
url = require('url'),
app = express();

// Settings
var settings = {
node_port: process.argv[2] || 8000,
uploadPath: __dirname + '/uploads/'
};

app.set('views', __dirname + '/views');
app.set('view engine', 'jade');

app.use(express.static(__dirname + '/public'));

app.use(express.bodyParser({uploadDir: settings.uploadPath}));

app.get('/', function(request, response) {
response.render('index');
})

app.post('/upload', function(request, response, next) {
// the uploadDir is typically used as a temp save location, but we are just going to use the same directory to
// store the final file.

var savePath = settings.uploadPath;

var fileName = request.files.qqfile.name;

//after upload, rename the file and respond to Fine Uploader to notify it of success
fs.rename(request.files.qqfile.path, savePath + fileName, function(err) {
if (err != null) {
console.log('Err: ' + err);
response.send(JSON.stringify({success: false, error: err}), {'Content-Type': 'text/plain'}, 200);
}
else {
response.send(JSON.stringify({success: true}), {'Content-Type': 'text/plain'}, 200);
console.log('File Uploaded: ' + savePath + fileName);
}
})

});



// Starting the express server
app.listen(settings.node_port, '127.0.0.1');
console.log("Express server listening on %s:%d for uploads", '127.0.0.1', settings.node_port);
1 change: 1 addition & 0 deletions nodejs/public/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The code assumes that the Fine Uploader zip including .js and .css files is available here in a directory called 'fine-uploader-jquery'.
1 change: 1 addition & 0 deletions nodejs/uploads/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*
32 changes: 32 additions & 0 deletions nodejs/views/index.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
!!!
html
head
block head
title Fine Uploader
link(rel='stylesheet', href='fine-uploader-jquery/fineuploader-3.4.1.css')
script(src='//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js')
script(src='fine-uploader-jquery/jquery.fineuploader-3.4.1.min.js')
body
block content

div.container
div.row
h3 Fine Uploader Node Example
#jquery-wrapped-fine-uploader
script(type='text/javascript')
jQuery(document).ready(function() {
jQuery('#jquery-wrapped-fine-uploader').fineUploader({
debug: true,
request: {
endpoint: '/upload',
params: {},
chunking: {
enabled: true,
},
resume: {
enabled: true,
},
},
});
});

0 comments on commit 3822b5e

Please sign in to comment.