当我尝试将照片上传到服务时,我的Open shift节点服务器崩溃了。处理此问题的代码是
var express = require('express');
var app = express();
var server = app.listen(process.env.OPENSHIFT_NODEJS_PORT || 8080, process.env.OPENSHIFT_NODEJS_IP);
var io = require('socket.io').listen(server);
var url = require("url");
var cookieParser = require('cookie-parser');
var fs = require('fs')
//Databse Loading
var Datastore = require('nedb')
, db = new Datastore({ filename: './db/users.json', autoload: true });
postsDB = new Datastore({ filename: './db/posts.json', autoload: true });
followDB = new Datastore({ filename: './db/follow.json', autoload: true });
likeDB = new Datastore({ filename: './db/like.json', autoload: true });
notifDB = new Datastore({ filename: './db/notif.json', autoload: true });
app.use(express.static('frontend'));
app.use(express.static('frontend/uploads'));
app.use('/user', express.static(__dirname + '/public'))
app.use('/uploads', express.static(process.env.OPENSHIFT_DATA_DIR+'/uploads'));
app.use('/uploads', express.static(__dirname + '/uploads'))
只使用fs包上传图片。
处理图片展示位置的代码如下
socket.on('Image Post', function(ext, buffer, location, postData) {
console.log(ext);
postsDB.insert([postData], function (err, newDocs) {
if(err){console.log(err)}
else{
console.log(newDocs);
var fileName = process.env.OPENSHIFT_DATA_DIR + '/frontend/uploads' + "/" + newDocs[0]._id + ext;
fs.open(fileName, 'a', 0755, function(err, fd) {
if (err) throw err;
fs.write(fd, buffer, null, 'Binary', function(err, written, buff) {
fs.close(fd, function() {
console.log('File saved successful!');
});
});
});
}
})
});
再次感谢。