Generate .ics url like Basecamp in Nodejs - node.js

I am working on nodeJs(backend) and (Angular)
I want to generate a .ics file URL for google calendar and for Apple and Microsoft as a downloadable file.
I know there is a node module ics and I am using that, but that only creates a .ics file I want this to be unique for each user and also want this to delete automatically.
Also, it should automatically sync with the events added.
any suggestion for this?

I have been unable to find a way that would continually update someone's schedule since this would require continuous access to someone's calendar. While building a scheduling site I got around your storage and unique file problem by using a brute force solution.
First, as a client access the download endpoint an unique ics file is generated and stored as schedule_date_client-name.ics. This unique file is then sent to the user using res.download and promptly deleted using fs.unlink(path_to_file).
Here is an example of this in action :
try {
res.download(path, function(error){
if (error) {
console.log("Error : ", error)
}
fs.unlink(path, (error) => {
// log any error
if (error) {
console.log(error);
}
})
});
} catch (e) {
next(e);
}
The best way I found around this is to generate the .ics file as the user accesses a /download url endpoint. Send the file in a downloadable format using res.download in your controller file. Here is more information on the packages I used for this solution :
Node .ics : https://www.npmjs.com/package/ical-generator
Node file : https://nodejs.dev/learn/the-nodejs-fs-module
Require the modules :
const ical = require('ical-generator');
const fs = require("fs");

Related

how to read the attachments stored using multer - nodejs/expressjs

I am new to node programming, I am trying to build an app which will be having a feature where users will be uploading there docs/images and then afterwards they can also view the same files which they had uploaded earlier.
I came across multer with which I built an API which stores the attached file on the server, please refer below code :
app.post('/submit-form', uploadPlugin.single('files'),(req, res) => {
console.log(req.file);
if (!req.file){
res.status(400).json({"error":"Something went wrong while uploading file"})
return;
}
else
{
path = "C:\\Users\\APIs" + req.file.path
console.log(path)
res.sendFile(path);
}
})
Now my problem is if at all I need to view the attached files, how can I do so (For ex: if someone uploads an Image it should show this image was uploaded)
I am trying to send the file as the response but in the HTML page it just shows gibberish (probably the byte stream, refer the image attached) instead it should display the actual document say image for example.

On trying to download file from Node JS, In browser getting error as: Failed network error

I am trying to download a file located in my storage in box.com.
When user made a api request with specific id then i am getting file url related to that id from box.com and in node js again making another api call to get the file chunks and returning it to the client.
Process is:
User make an api call as: /api?id=123 to node js
In node js, I am getting file url related to id 124.
Obtained file url is: https://box.com/xyz_123
Using this file url, I am making again an api call to the box server and getting file as chunk by chunk and returning it to client.
When the file is about to finish in 5 seconds, then in browser it is showing error as: Failed network error.
Here is the way i am calling the box url to get file streams and returning to client side chunk by chunk.
const creq = https.request(boxurl, (cres) => {
cres.setEncoding('utf8');
res.writeHead(cres.statusCode,
{
'Content-Length': cres.headers['content-length'],
'Content-Type': 'application/octet-stream',
'Content-Disposition' : cres.headers['content-disposition']
}
);
// wait for data
cres.on('data', function(chunk){
res.write(chunk);
});
cres.on('close', function(){
// closed, let's end client request as well
return res.end('Success');
});
cres.on('end', function(){
// finished, let's finish client request as well
return res.end('Success');
});
}).on('error', function(e) {
// we got an error, return 500 error to client and log error
return res.end(e.message);
});
creq.end();
File download mechanism working very well in my system.
When i deployed code to remote server and checking, then only this error is appearing.
Can any one please help me on this. How to solve this.
Thanks in advance.
To make use of the Box Node SDK you have to create a Box app in the Box Developer Console. Then you need to configure and authorise that app. An example can be found here
You can now write your node app that uses the Box SDK to talk to your Box app / account. An example of how to connect it up can be found here.
The code to download a file from Box can be found from Box's SDK documentation here. But before you can download a file you will need to discover its File Id by using other SDK methods such as getting a folder's items or search for content.
The folder id of your root folder in Box is always 0 which is a good place to start when searching for various file or folder id numbers.

file download from ftp server in nodejs

I have web application where on button click invoking REST API which fetch list of files available in ftp server and displayed in div with hyperlink on file name as below.
for this I am using jsftp module.
ftp.ls(".", function(err, res) {
res.forEach(function(file) {
console.log(file.name);
//logic to display in div
});
});
I have another REST API for download file from FTP server which invoked on file name click present in div but it is downloaded in local directory where web application deploy and not in users system with below code, but I want when user click on file name that file should download in users system or computer. How I can achieve this.
ftp.get('remote/file.txt', 'local/file.txt', function(hadErr) {
if (hadErr)
console.error('There was an error retrieving the file.');
else
console.log('File copied successfully!');
});
Now that the file is on your machine you will want to actually server the file to the client when they click on the link. Using res.write something like: res.write(file, 'binary');
Here are some concepts that you'll find useful: Node.js send file to client

Simple file upload with Node.js

I am building a node.js application that uses pdf.js to read pdf files, but much like other js, pdf.js does not allow cross origin requests. So, I need a way to save files selected with a file input to my pdf directory. I'm not so great with node so make it as simple as possible if you can.
Here is a basic idea of what you need:
1st, require and use module 'connect-multiparty'. This will expose the req.files object in node.
var multipart = require('connect-multiparty');
app.use(multiparty({});
Then, in your controller method, require the 'fs' module, and use it to save the uploaded file.
var fs = require('fs');
fs.writeFileSync("myFileName", req.files.file.ws.path, function(err) {
if(err) { console.log(err); }
else { console.log("file uploaded"); }
});
Being familiar with node will help, but the two basic libraries you need to perform this are the aforementioned https://www.npmjs.com/package/connect-multiparty and http://nodejs.org/api/fs.html
edit: see the link in the comments below. this answer is incomplete and is better explained in the link

Nodejs - writing and saving files

I am investigating how to download files to a user's local machine but I'm not quite sure what I need in order to do this. I'm using Nodejs and Express with Angularjs on the front-end.
User's can write text into a textarea and it's this text that will be written to the file.
To do this I have:
...
fs = require('fs');
fs.writeFile('filename.txt', textarea.text, function (err) {
if (err) return console.log(err);
res.send(200);
});
...
Once the file is created how do I get it to download on the user's machinea?
Use res.download
res.download('filename.txt');
http://expressjs.com/4x/api.html#res.download
If you don't need to store the file on the server, you could just sent it back to the user directly:
res.attachment('filename.txt');
res.set('Content-Type', 'text/plain');
res.send(textarea.text);
This is not only simpler but also improves performance (no disk i/o) and more secure (no untrusted files on your server).

Resources