我目前正在开发一个node.js bluemix应用程序,该应用程序提供api图像/文件上传,然后使用mongodb服务处理图像。在本地,这项服务非常有效。不幸的是,该服务无法访问bluemix上的上传文件? - 如何解决这个问题?提前谢谢
require('rootpath')();
var express = require('express');
var app = express();
var session = require('express-session');
var bodyParser = require('body-parser');
var expressJwt = require('express-jwt');
var config = require('config.json');
var cookieParser = require('cookie-parser');
var helmet = require('helmet');
var cors = require('cors');
var md5 = require('md5');
var sha1 = require('js-sha1');
var sha256 = require('js-sha256');
var crypto = require('crypto');
var path = require('path');
var multer = require('multer');
var mkdirp = require('mkdirp');
var storage = multer.diskStorage({
destination: function(req, file, cb) {
var dir = './uploads/' + req.query.projectId + '/' + req.query.milestoneId + '/';
mkdirp(dir, function(err) {
if (err) {
console.error(err);
}
// move cb to here
cb(null, dir);
});
console.log("Upload: saved to " + dir + file.originalname);
},
filename: function(req, file, cb) {
cb(null, file.originalname);
}
})
var upload = multer({ storage: storage })
var genRandomString = function(length) {
return crypto.randomBytes(Math.ceil(length / 2))
.toString('hex') /** convert to hexadecimal format */
.slice(0, length); /** return required number of characters */
};
var sha512 = function(password, salt) {
var hash = crypto.createHmac('sha512', salt); /** Hashing algorithm sha512 */
hash.update(password);
var value = hash.digest('hex');
return {
salt: salt,
passwordHash: value
};
};
function saltHashPassword(userpassword) {
var salt = genRandomString(16); /** Gives us salt of length 16 */
var passwordData = sha512(userpassword, salt);
console.log('UserPassword = ' + userpassword);
console.log('Passwordhash = ' + passwordData.passwordHash);
console.log('nSalt = ' + passwordData.salt);
}
//saltHashPassword('MYPASSWORD');
//saltHashPassword('MYPASSWORD');
app.use(cors());
app.use(helmet());
app.use(cookieParser());
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(session({ secret: config.secret, resave: false, saveUninitialized: true }));
// use JWT auth to secure the api
app.use('/api', expressJwt({ secret: config.secret }).unless({ path: ['/api/users/authenticate', '/api/users/register'] }));
// routes
app.use('/api/fileuploads', require('./controllers/api/svr.fileupload.controller'));
app.use('/api/session', require('./controllers/api/svr.session.controller'));
app.use('/api/uploadImage', require('./controllers/api/svr.imageUpload.controller'));
app.use('/js', express.static(__dirname + '/node_modules/bootstrap/dist/js')); // redirect bootstrap JS
app.use('/js', express.static(__dirname + '/node_modules/jquery/dist')); // redirect JS jQuery
app.use('/css', express.static(__dirname + '/node_modules/bootstrap/dist/css')); // redirect CSS bootstrap
app.use('/img', express.static(__dirname + '/views'));
app.use('/img/project', express.static(__dirname + '/images'));
app.use('/file/project', express.static(__dirname + '/files'));
app.use('/uploadDoc', express.static(__dirname + '/uploads'));
app.use('/uploadImage', express.static(__dirname + '/images'));
app.post('/savedata', upload.single('file'), function(req, res, next) {
console.log('Upload Successful req.file = ', req);
console.log('Upload Successful req.body = ', req.body);
var hash1 = md5(req.file);
console.log('md5=', hash1);
saltHashPassword(hash1);
return res.send(hash1);
});
// make '/app' default route
app.get('/', function(req, res) {
return res.redirect('/app');
});
// start server
var server = app.listen(process.env.PORT || 3000, function() {
console.log('Server listening at http://' + server.address().address + ':' + server.address().port);
});