Skip to content

Instantly share code, notes, and snippets.

@mattn9x
Created July 9, 2017 08:01
Show Gist options
  • Save mattn9x/4761e8ee017eaf5eb99825ed103e6b39 to your computer and use it in GitHub Desktop.
Save mattn9x/4761e8ee017eaf5eb99825ed103e6b39 to your computer and use it in GitHub Desktop.
config router với trường hợp create link
router.get('/create-link', function (req, res) {
let domain = "http://localhost:82";
let dataExcel = [];
User.find({})
.then(users => {
let demo2 = [];
Object.keys(users[0]['_doc']).forEach(key => {
"use strict";
demo2.push(key);
});
dataExcel.push(demo2);
for (let item of users) {
let demo3 = [];
Object.keys(item._doc).forEach(key => {
demo3.push(item[key]);
});
dataExcel.push(demo3);
}
let buffer = nodeXlsx.build([{name: "List User", data: dataExcel}]);
// Thay vì res.attachment('user.xlsx') mình sẽ ghi ra file users.xlsx theo đường dẫn public/exports
// Sau đó mình tạo 1 liên kết trỏ tới file đó và gửi nó sang trang receive đính kèm theo link
fs.writeFile('public/exports/users.xlsx', buffer, function (err) {
if (err) return console.log(err);
else return res.render('receive', {link: `${domain}/exports/users.xlsx`});
});
})
.catch(err => res.status(400).json(err));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment