- 
      
- 
        Save falexandre/02927d533786581bc5ff7653da243a49 to your computer and use it in GitHub Desktop. 
    Upload and display image with NodeJS and Express.
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | /* | |
| * Module dependencies. | |
| */ | |
| var express = require('express') | |
| , routes = require('./routes') | |
| , user = require('./routes/user') | |
| , common = require('./routes/common') | |
| , fs = require('fs') | |
| , http = require('http') | |
| , util = require('util') | |
| , path = require('path'); | |
| var app = express(); | |
| /* | |
| * connect middleware - please note not all the following are needed for the specifics of this example but are generally used. | |
| */ | |
| app.configure(function() { | |
| app.set('port', process.env.PORT || 3000); | |
| app.set('views', __dirname + '/views'); | |
| app.use(express.favicon()); | |
| app.use(express.logger('dev')); | |
| app.use(express.bodyParser({ keepExtensions: true, uploadDir: __dirname + '/public/uploads' })); | |
| app.use(express.methodOverride()); | |
| app.use(app.router); | |
| app.use(express.static(path.join(__dirname, '/public'))); | |
| app.use(express.static(__dirname + '/static')); | |
| app.use(express.errorHandler()); | |
| }); | |
| //File upload | |
| app.get('/upload', common.imageForm); | |
| app.post('/upload', common.uploadImage); | |
| http.createServer(app).listen(app.get('port'), function(){ | |
| console.log('Express server listening on port ' + app.get('port')); | |
| }); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | var util = require('util'); | |
| exports.imageForm = function(req, res) { | |
| res.render('upload', { | |
| title: 'Upload Images' | |
| }); | |
| }; | |
| exports.uploadImage = function(req, res, next){ | |
| console.log('file info: ',req.files.image); | |
| //split the url into an array and then get the last chunk and render it out in the send req. | |
| var pathArray = req.files.image.path.split( '/' ); | |
| res.send(util.format(' Task Complete \n uploaded %s (%d Kb) to %s as %s' | |
| , req.files.image.name | |
| , req.files.image.size / 1024 | 0 | |
| , req.files.image.path | |
| , req.body.title | |
| , req.files.image | |
| , '<img src="uploads/' + pathArray[(pathArray.length - 1)] + '">' | |
| )); | |
| }; | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | extends layout | |
| block content | |
| form(action="/upload", method="post", enctype="multipart/form-data") | |
| input(type="text", name="title") | |
| input(type="file", multiple, name="image") | |
| button(type="submit", value="Upload") Upload File | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment