I currently have a webapp using Express, Node, Ember, and Mongo. Ember app resides in a project folder (./public/index.html) in the root of the node/express install. I have express set to serve static files from the public directory and render index.html to any get requests so the ember app is accessible.
I have a route/view in my Ember app that has a form that accepts a file upload, this form's action is a post request to an express route that performs calculations and queries a local mysql database with updates. The function works fine but at the end of the .post express function when I res.json or res.send the response appears in the browser window and clears out my ember view.
I assume the correct way to handle this is to res.render('view',{data:'Finished processing file'});
then display the data value on the ember template. Question is how can I render an ember view with express. I added express-handlebars to my project and setup the view engine correctly but I don't know how to associate ember views with express so it knows how to render the correct view with response data.
hbs file for the ember view
<div class="col-md-8 col-md-offset-2 text-center">
<h2 class="toolTitle">Reactivate SKUs</h2>
<p class="lead">CSV Should Contain 1 Column (SKU) Only</p>
<form action="/tools/sku/reactivate" method="POST" enctype="multipart/form-data">
<input class="center-block" type="file" name="csvdata">
<button type="submit" class="btn btn-md btn-danger">Submit</button>
</form>
</div>
router.js(express router)
var quotes = require('../api/quote');
var cors = require('cors');
var sku = require('../api/tools/sku');
var multer = require('multer');
var upload = multer({ dest: 'uploads/' });
var util = require("util");
var fs = require("fs");
var corsOptions = {
origin: 'http://localhost:4200'
}
module.exports = function(router){
router.route('/quotes').post(cors(corsOptions),function(req,res){
console.log(req.body);
quotes.addQuote(req,res);
}).get(cors(corsOptions),function(req,res){
quotes.getAllQuotes(req,res);
});
router.route('*').get(cors(corsOptions), function(req,res){
res.sendFile('public/index.html',{root:'./'});
});
router.route('/tools/sku/reactivate').post(upload.single('csvdata'),function(req,res){
console.log('handing request over to sku.reactivate');
sku.reactivate(req,res);
});
};
sku.js express function
var mysql = require('mysql');
var csv = require('csv-parse');
var multer = require('multer');
var fs = require('fs');
//mysql setup
const connection = mysql.createConnection(
{
host : 'localhost',
user : 'rugs_remote2',
password : 'ofbiz',
database : 'rugs_prd2',
multipleStatements: true
}
);
connection.connect();
module.exports.reactivate = function(req,res){
//define mysql query function for once readStream emits end event
function reactivationQuery(arr){
console.log(arr);
const queryString = "UPDATE PRODUCT SET SALES_DISCONTINUATION_DATE = NULL WHERE PRODUCT_ID IN (?)";
connection.query(queryString,arr,function(err,rows,fields){
console.log(rows,fields);
if(err){
console.log('Error running sku.reactivate module error is: '+err);
}
res.send('DONE');
});
}
//define array for holding csv data in this case skus
const skuArray = [];
//define filesystem readstream from uploaded file
let readStream = fs.createReadStream(req.file.path).pipe(csv());
//push csv data to array ignoring headers to skuArray
readStream.on('data', function(chunk){
if(chunk[0] !== 'SKU'){
skuArray.push(chunk[0]);
}
});
//error handling
readStream.on('error',function(err){
console.log('Error while reading file stream [ERROR] '+ err);
res.send('Error while processing file');
});
//once read is finished map skuArray to usable string for IN Clause
readStream.on('end',function(){
let stringifyArray = skuArray;
stringifyArray = [stringifyArray];
reactivationQuery(stringifyArray);
});
}
Aucun commentaire:
Enregistrer un commentaire