这是我的app.js文件:
这是一个upload.js文件:
var express = require('express')
, multiparty = require('multiparty')
, request = require('request')
, fs = require('fs')
, util = require('util')
, http = require('http');
var app = express();
app.use('/static', express.static('static'));
process.on('uncaughtException', function (err) {
console.log(err);
});
app.get('/', function (req, res) {
res.redirect('static/index.html');
});
app.post('/upload', function(req, res, next){
//https://github.com/request/request#streaming
var form = new multiparty.Form();
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: files}));
});
form.on('file', function(name, file) {
var formData = {
file: {
value: fs.createReadStream(file.path),
options: {
filename: file.originalFilename
}
}
};
console.log(formData)
// Post the file to the upload server
request.post({url: 'http://192.138.2.73:4000/upload', formData: formData});
});
});
var server = app.listen(3000, '0.0.0.0' ,function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
这是一个upload.js文件:
var express = require('express')
, multiparty = require('multiparty')
, cors = require('cors')
, util = require('util')
, app = express();
app.use(cors());
process.on('uncaughtException', function (err) {
console.log(err);
});
app.get('/', cors(), function(req, res, next){
res.json({msg: 'This is CORS-enabled for all origins!'});
});
app.post('/upload', cors(), function(req, res, next){
var form = new multiparty.Form();
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: files}));
});
form.on('file', function(name,file) {
// console.log(file);
// console.log(name);
});
});
app.listen(4000, function(){
console.log('CORS-enabled web server listening on port 4000');
});
我正在将文件从一个系统上传到另一个远程系统。由于我在远程系统中运行了upload.js,而在我的系统中运行了app.js,并且我正在从我的系统上传文件,所以文件成功地上传到了另一个系统。现在我的问题是,我上传的所有文件都保存在远程系统的'/var/tmp'和我的系统的'/var/tmp'中。我需要更改此路径为我自己定义的路径,例如:/var/www/html/myfolder。然后文件就不应该保存在我的系统中。