Page tree
Skip to end of metadata
Go to start of metadata

The following script is a simple Node.js application that creates an HTTP server, and handles image uploads (multipart form posts) suitable for use with Textbox.io. Please note that this script is provided for your reference only.

  File Modified
JavaScript File postAcceptor.js Apr 08, 2015 by Gavin Douglas

 

Node.js Post Acceptor
var http = require('http');
var fs = require('fs');
var multiparty = require('multiparty'); // https://www.npmjs.com/package/multiparty

// #### #### #### #### #### #### #### #### ####
//  Setup the Textbox.io Example Post Acceptor
// #### #### #### #### #### #### #### #### #### 
// Set the URL and port for the post acceptor
var acceptorPath = '/upload';
var acceptorPort = 8080;
// Only these origins will be allowed to upload images
var allowedOrigins = ['http://localhost', 'http://192.168.1.1'];
// Set the upload directory
// Notw: This string is prepended to the filename to construct
// the image url returned to the Textbox.io editor, like: uploadDirectory/filename.png
var uploadDirectory = 'images/';
// Set the resource URL for from which stored images will be served.
var resourceUrl = 'http://localhost/';
// #### #### #### #### #### #### #### #### #### 
// #### #### #### #### #### #### #### #### #### 

http.createServer(function(req, res) {
  var origin = req.headers.origin;
  if(allowedOrigins.indexOf(origin) == -1) { 
    // Deny invalid origins
    res.writeHead(403, { 'HTTP/1.0 403 Origin Denied' : origin });
    res.end();
  } else {
    // For valid origins check path and method
    if (req.url === acceptorPath && req.method === 'POST') {
      var form = new multiparty.Form();
      form.parse(req);
      form.on('file', function(name, file) {
        var saveFilePath = uploadDirectory + file.originalFilename;
        fs.rename(file.path, saveFilePath, function(err) {
          if (err) {
            // Handle problems with file saving
            res.writeHead(500);
            res.end();
          } else {
            // Respond to the successful upload with JSON.
            // Use a location key to specify the path to the saved image resource.
            // { location : '/your/uploaded/image/file'}
            var textboxResponse = JSON.stringify({ 
              location : saveFilePath 
            });
            
            // If your script needs to receive cookies, set images.upload.credentials:true in
            // the Textbox.io configuration and enable the following two headers.
            // res.setHeader('Access-Control-Allow-Credentials', 'true');
            // res.setHeader('P3P', 'CP="There is no P3P policy."');
            res.statusCode = 200;
            res.setHeader('Access-Control-Allow-Origin', origin);
            res.end(textboxResponse);
          } 
        });
      });
      form.on('error', function(err) {
        res.writeHead(500);
        res.end();
      });
      return;
    
    } else {
      // Return 404 for requests for other paths/methods
      res.writeHead(404);
      res.end();
    }
  }
}).listen(acceptorPort);
  • No labels