这是我的 app.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。然后文件不应该保存在我的系统中。