how to read posted data on node js api





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















i want to read the csv data uploaded to backened.



for this i am sending the data via post from front end..



frontend code:



fileEvent(e) {
this.filedata = e.target.files;
if (this.filedata.length > 0) {
const file: File = this.filedata[0];
console.log(file);
const formData: FormData = new FormData();
formData.append('files', file, file.name);
this.http.post('myUrl', {file: formData}, this.options)
.subscribe((res) => {
});
}
}


screenshot of my file:
enter image description here



now on backened i have written route on api.js that directs me
to the controller i have created.



my api.js code:



router.post('/product/csvdata', function (req, res) {
productimport.importcsvProduct(req, res);
});


and finally on my controller i am consoling my data:



    var product = {
importcsvProduct: function (req,res) {
console.log(req.body.file);
}
};

module.exports = product;


but i am getting empty {} in console..??



can anyone check whats wrong with this..??










share|improve this question

























  • you may need body-parser as a middleware and configure it to accept form data

    – pandamakes
    Nov 16 '18 at 12:49











  • Is this helps you to solve the problem?

    – lependu
    Nov 16 '18 at 12:49


















0















i want to read the csv data uploaded to backened.



for this i am sending the data via post from front end..



frontend code:



fileEvent(e) {
this.filedata = e.target.files;
if (this.filedata.length > 0) {
const file: File = this.filedata[0];
console.log(file);
const formData: FormData = new FormData();
formData.append('files', file, file.name);
this.http.post('myUrl', {file: formData}, this.options)
.subscribe((res) => {
});
}
}


screenshot of my file:
enter image description here



now on backened i have written route on api.js that directs me
to the controller i have created.



my api.js code:



router.post('/product/csvdata', function (req, res) {
productimport.importcsvProduct(req, res);
});


and finally on my controller i am consoling my data:



    var product = {
importcsvProduct: function (req,res) {
console.log(req.body.file);
}
};

module.exports = product;


but i am getting empty {} in console..??



can anyone check whats wrong with this..??










share|improve this question

























  • you may need body-parser as a middleware and configure it to accept form data

    – pandamakes
    Nov 16 '18 at 12:49











  • Is this helps you to solve the problem?

    – lependu
    Nov 16 '18 at 12:49














0












0








0








i want to read the csv data uploaded to backened.



for this i am sending the data via post from front end..



frontend code:



fileEvent(e) {
this.filedata = e.target.files;
if (this.filedata.length > 0) {
const file: File = this.filedata[0];
console.log(file);
const formData: FormData = new FormData();
formData.append('files', file, file.name);
this.http.post('myUrl', {file: formData}, this.options)
.subscribe((res) => {
});
}
}


screenshot of my file:
enter image description here



now on backened i have written route on api.js that directs me
to the controller i have created.



my api.js code:



router.post('/product/csvdata', function (req, res) {
productimport.importcsvProduct(req, res);
});


and finally on my controller i am consoling my data:



    var product = {
importcsvProduct: function (req,res) {
console.log(req.body.file);
}
};

module.exports = product;


but i am getting empty {} in console..??



can anyone check whats wrong with this..??










share|improve this question
















i want to read the csv data uploaded to backened.



for this i am sending the data via post from front end..



frontend code:



fileEvent(e) {
this.filedata = e.target.files;
if (this.filedata.length > 0) {
const file: File = this.filedata[0];
console.log(file);
const formData: FormData = new FormData();
formData.append('files', file, file.name);
this.http.post('myUrl', {file: formData}, this.options)
.subscribe((res) => {
});
}
}


screenshot of my file:
enter image description here



now on backened i have written route on api.js that directs me
to the controller i have created.



my api.js code:



router.post('/product/csvdata', function (req, res) {
productimport.importcsvProduct(req, res);
});


and finally on my controller i am consoling my data:



    var product = {
importcsvProduct: function (req,res) {
console.log(req.body.file);
}
};

module.exports = product;


but i am getting empty {} in console..??



can anyone check whats wrong with this..??







node.js angular rest api form-data






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 12:46







Deb

















asked Nov 16 '18 at 12:30









DebDeb

209




209













  • you may need body-parser as a middleware and configure it to accept form data

    – pandamakes
    Nov 16 '18 at 12:49











  • Is this helps you to solve the problem?

    – lependu
    Nov 16 '18 at 12:49



















  • you may need body-parser as a middleware and configure it to accept form data

    – pandamakes
    Nov 16 '18 at 12:49











  • Is this helps you to solve the problem?

    – lependu
    Nov 16 '18 at 12:49

















you may need body-parser as a middleware and configure it to accept form data

– pandamakes
Nov 16 '18 at 12:49





you may need body-parser as a middleware and configure it to accept form data

– pandamakes
Nov 16 '18 at 12:49













Is this helps you to solve the problem?

– lependu
Nov 16 '18 at 12:49





Is this helps you to solve the problem?

– lependu
Nov 16 '18 at 12:49












1 Answer
1






active

oldest

votes


















0














You need to use a file handling middleware in this case, such as multer.



const express = require('express')
const multer = require('multer')
const upload = multer({ dest: 'uploads/' })

const app = express()

app.post('/profile', upload.single('csvdata'), function (req, res, next) {
// req.file is the `csvdata` file
// req.body will hold the text fields, if there were any
})





share|improve this answer
























  • showing Cannot POST /api/product/csvdata on frontend

    – Deb
    Nov 17 '18 at 5:53













  • Can you update the question with the new code you're using?

    – Paul
    Nov 17 '18 at 7:10












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53337967%2fhow-to-read-posted-data-on-node-js-api%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You need to use a file handling middleware in this case, such as multer.



const express = require('express')
const multer = require('multer')
const upload = multer({ dest: 'uploads/' })

const app = express()

app.post('/profile', upload.single('csvdata'), function (req, res, next) {
// req.file is the `csvdata` file
// req.body will hold the text fields, if there were any
})





share|improve this answer
























  • showing Cannot POST /api/product/csvdata on frontend

    – Deb
    Nov 17 '18 at 5:53













  • Can you update the question with the new code you're using?

    – Paul
    Nov 17 '18 at 7:10
















0














You need to use a file handling middleware in this case, such as multer.



const express = require('express')
const multer = require('multer')
const upload = multer({ dest: 'uploads/' })

const app = express()

app.post('/profile', upload.single('csvdata'), function (req, res, next) {
// req.file is the `csvdata` file
// req.body will hold the text fields, if there were any
})





share|improve this answer
























  • showing Cannot POST /api/product/csvdata on frontend

    – Deb
    Nov 17 '18 at 5:53













  • Can you update the question with the new code you're using?

    – Paul
    Nov 17 '18 at 7:10














0












0








0







You need to use a file handling middleware in this case, such as multer.



const express = require('express')
const multer = require('multer')
const upload = multer({ dest: 'uploads/' })

const app = express()

app.post('/profile', upload.single('csvdata'), function (req, res, next) {
// req.file is the `csvdata` file
// req.body will hold the text fields, if there were any
})





share|improve this answer













You need to use a file handling middleware in this case, such as multer.



const express = require('express')
const multer = require('multer')
const upload = multer({ dest: 'uploads/' })

const app = express()

app.post('/profile', upload.single('csvdata'), function (req, res, next) {
// req.file is the `csvdata` file
// req.body will hold the text fields, if there were any
})






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 12:53









PaulPaul

27k86294




27k86294













  • showing Cannot POST /api/product/csvdata on frontend

    – Deb
    Nov 17 '18 at 5:53













  • Can you update the question with the new code you're using?

    – Paul
    Nov 17 '18 at 7:10



















  • showing Cannot POST /api/product/csvdata on frontend

    – Deb
    Nov 17 '18 at 5:53













  • Can you update the question with the new code you're using?

    – Paul
    Nov 17 '18 at 7:10

















showing Cannot POST /api/product/csvdata on frontend

– Deb
Nov 17 '18 at 5:53







showing Cannot POST /api/product/csvdata on frontend

– Deb
Nov 17 '18 at 5:53















Can you update the question with the new code you're using?

– Paul
Nov 17 '18 at 7:10





Can you update the question with the new code you're using?

– Paul
Nov 17 '18 at 7:10




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53337967%2fhow-to-read-posted-data-on-node-js-api%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma