Express socket.io强大的mean.io陷入了如何整合表单进度

时间:2014-03-12 11:52:34

标签: node.js express socket.io formidable

我正在研究mean.io

server.js

'use strict';

/**
 * Module dependencies.
 */
var express = require('express'),
    fs = require('fs'),
    passport = require('passport'),
    logger = require('mean-logger');


/**
 * Main application entry file.
 * Please note that the order of loading is important.
 */

//Load configurations
//Set the node enviornment variable if not set before
process.env.NODE_ENV = process.env.NODE_ENV || 'development';

//Initializing system variables 
var config = require('./config/config'),
    auth = require('./config/middlewares/authorization'),
    mongoose = require('mongoose');

//Use only for development env
if (process.env.NODE_ENV === 'development') {
    //mongoose.set('debug', true);
}
// set temp directory for uploads
process.env.TMPDIR = config.tmp;



//Bootstrap db connection
var db = mongoose.connect(config.db);

//Bootstrap models
var modelsPath = __dirname + '/app/models';
var walk = function(path) {
    fs.readdirSync(path).forEach(function(file) {
        var newPath = path + '/' + file;
        var stat = fs.statSync(newPath);
        if (stat.isFile()) {
            if (/(.*)\.(js$|coffee$)/.test(file)) {
                require(newPath);
            }
        } else if (stat.isDirectory()) {
            walk(newPath);
        }
    });
};
walk(modelsPath);

//bootstrap passport config
require('./config/passport')(passport);

var app = express()
    , http = require('http')
    , server = http.createServer(app)
    , io = require('socket.io').listen(server);

//express settings
require('./config/express')(app,passport,db);

//Bootstrap routes
require('./config/routes')(app,passport,auth);

//Start the app by listening on <port>
var port = process.env.PORT || config.port;
server.listen(port);

io.sockets.on('connection', function (socket){

    socket.on('addPost', function (data) {
        socket.broadcast.emit('addedPost', data);
    });
});

console.log('Express app started on port ' + port);

//Initializing logger
logger.init(app,passport ,mongoose);

//expose app
exports = module.exports = app;

routes.js

var postController = require('../app/controllers/api/post');

应用程序/控制器/ API / post.js

exports.upload = function(req, res,next) {
    var w = 200;
    var h = 75;
    var form = new formidable.IncomingForm;
    form.on('progress', function(bytesReceived, bytesExpected){
        var percent = Math.floor(bytesReceived / bytesExpected * 100);
        // here is where you can relay the uploaded percentage using Socket.IO
        // How can got socket here !
        socket.emit('progress', { percent: percent });
    });
    form.parse(req, function(err, fields, files){
        if(err){
            res.jsonp(500, err.message);
        } 

    });
};

我找不到获取套接字的方法 在exports.upload任何想法?

0 个答案:

没有答案