ist, wie Sie es tun mit express und body-parser Modul die Post-Anforderung zu analysieren und holen Sie die Dateien, die Sie benötigen .Dies ist, was in Ihrem node.js Server geht.
Import alle Module:
var express = require("express");
var bodyParser = require('body-parser')
var app = express(); //init express app()
var util = require('util');
//APP CONFIGURATION >> Skip this if you dont want CORS
app.use(express.static('app')); // use this as resource directory
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
Konfigurieren Sie den Beitrag Url:
//url => url/for/mypostrequest
app.post(url, done, function (req, res) {
//Handle the post request body here...
var filesUploaded = 0;
//check if files present
if (Object.keys(req.files).length === 0) {
console.log('no files uploaded');
} else {
console.log(req.files);
var files = req.files.file1;
//If multiple files store in array..
if (!util.isArray(req.files.file1)) {
files = [req.files.file1];
}
filesUploaded = files.length;
}
res.json({message: 'Finished! Uploaded ' + filesUploaded + ' files. Route is /files1'});
});
Sicherstellen, dass alle Module installiert sind und vorhanden als Abhängigkeiten in package.json
CODE für einen API-Nachanruf vom Knoten ..
Fügen Sie das HTTP-Modul zuerst in Ihren Server ein.
var http = require('http');
var querystring = require('querystring');
var fs = require('fs');
Theninclude folgenden Code ein Post-Anforderung von dem Knoten-Server
var file1, file2;
//Read first File ...
fs.readFileSync('wheatData.csv', function (err, data) {
if (err) {
console.log('Error in file reading...');
}
file1 = data;
//Read second file....
fs.readFileSync('wheatData.csv', function (err, data) {
if (err) {
console.log('Error in file reading...');
}
file2 = data;
//Construct the post request data..
var postData = querystring.stringify({
'msg': 'Hello World!',
'file1': file1,
'file2': file2
});
var options = { //setup option for you request
hostname: 'www.path/to/api/',
port: 80,
path: '/upload',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
}
};
var req = http.request(options, function (res) {
console.log('STATUS:' + res.statusCode);
console.log('HEADERS:' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
res.on('end', function() {
console.log('No more data in response.');
});
});
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write(postData);
req.end();
});
});
Bitte beachten Sie, dass Code, um nicht auf Live-Server getestet wurde, müssen Sie möglicherweise Änderung wie pro Ihre Konfiguration machen.
Sie können auch andere Bibliotheken wie request oder needler..etc verwenden, um Postanrufe vom Knotenserver zu machen, wie in this Post vorgeschlagen.
Haben Sie einen Fehler erhalten und wenn ja, was genau ist der Fehler? –
Nun habe ich die Dateinamen am Anfang von 'form.append' in' file [] 'geändert und nun sagt mir mein Backend, dass ich nicht die richtige Anzahl von Dateien hochgeladen habe (was zwei ist). – foxes