Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

nodejs compat v0.4.2, new connect-static

  • Loading branch information...
commit d16af02fb481c4b73b17b928c6ff003c18022430 1 parent 6fabb70
@guybrush authored
Showing with 26 additions and 18 deletions.
  1. +5 −6 server/nodejs/README.md
  2. +21 −12 server/nodejs/server.js
View
11 server/nodejs/README.md
@@ -4,14 +4,13 @@ This is an example of using file-uploader with [node.js](http://nodejs.org).
It uses [connect](https://github.com/senchalabs/connect) for the sake of
simplicity. Multipart messages will be parsed with
[formidable](https://github.com/felixge/node-formidable) and the octet-stream
-will be written to disk directly (via the new awesome Stream-API of
-nodejs-v0.3.3).
+will be written to disk directly via the Stream-API of node.js.
-Installing node.js (v0.3.3+):
+Installing node.js (v0.4.2+):
- curl http://nodejs.org/dist/node-v0.3.3.tar.gz
- tar xzvf node-v0.3.3.tar.gz
- cd node-v0.3.3
+ curl http://nodejs.org/dist/node-v0.4.2.tar.gz
+ tar xzvf node-v0.4.2.tar.gz
+ cd node-v0.4.2
./configure && make && make install
Install modules (with npm):
View
33 server/nodejs/server.js
@@ -8,7 +8,7 @@ var connect = require('connect')
, formidable = require('formidable')
, uploadDir = __dirname+'/../uploads/'
, port = 3000
- , body, fileStream, server, uploader, form
+ , body, form, uploader
uploader = function(req, res, next) {
if (req.headers['content-type'].match(/multipart\/form-data/i)) {
@@ -17,7 +17,7 @@ uploader = function(req, res, next) {
form.uploadDir = uploadDir
form.parse(req, function(err, fields, files){
if (err) body = JSON.stringify({error:err})
- else body = '{"success":"true"}'
+ else body = JSON.stringify({success:true})
res.writeHead(200,
{ 'Content-Type':'text/html'
, 'Content-Length':body.length
@@ -26,18 +26,27 @@ uploader = function(req, res, next) {
})
}
else if (req.headers['content-type'].match(/application\/octet-stream/i)) {
- fileStream = fs.createWriteStream(uploadDir+req.headers['x-file-name'])
- req.pipe(fileStream)
- fileStream.on('error', function(err){
- body = JSON.stringify({error:err})
+ var fileStream = fs.createWriteStream(uploadDir+req.headers['x-file-name'])
+ //fileStream.setMaxListeners(100)
+ req.on('data',function(chunk) {
+ req.pause()
+ fileStream.write(chunk)
+ fileStream.on('drain',function() {
+ req.resume()
+ })
+ })
+ req.on('end',function() {
+ req.resume()
+ fileStream.destroySoon()
+ body = JSON.stringify({success:true})
res.writeHead(200,
{ 'Content-Type':'text/html'
, 'Content-Length':body.length
})
res.end(body)
})
- req.on('end', function() {
- body = '{"success":"true"}'
+ fileStream.on('error', function(err){
+ body = JSON.stringify({error:err})
res.writeHead(200,
{ 'Content-Type':'text/html'
, 'Content-Length':body.length
@@ -46,10 +55,10 @@ uploader = function(req, res, next) {
})
}
}
-
-server = module.exports = connect.createServer
- ( connect.staticProvider({root: __dirname+'/public', cache:true})
- , connect.staticProvider({root: __dirname+'/../../client', cache:true})
+
+var server = module.exports = connect
+ ( connect.static(__dirname+'/../../client')
+ , connect.static(__dirname+'/public')
, connect.router(function(app){app.post('/upload', uploader)})
).listen(port)
Please sign in to comment.
Something went wrong with that request. Please try again.