Is there any way I can display pdfs uploaded in my files using nodejs and ejs? - node.js

I created a way to upload files using Multer in my files then display their information to my frontend. My challenge is opening the uploaded PDF as when I click the link to open it says that it cannot get.

You need to specify your static files in Express.
Use app.use(express.static('public')) to specify that your static files are located on public folder.
And for get a file you don't need to add public in the url, for example:
http://localhost:3000/pdf/mypdf.pdf
See this documentation

Related

local a json file from Public folder

I am working a nodejs back-end project. So I have a json file tryList.json saved in public folder. Also in the folder, I have a html file. What should I put in the tag of the html file so that I can load the data from tryList.json?
I am trying to import the data into the front end js code, which will be used in data visualization later.
Thanks

Displaying images from node(server) to angular 2?

I'm uploading files from Angular 2 with ngx-uploader and storing them on backend (nodejs/feathers) with multer. Now I'm having trouble to reach and display them, for now im just trying to display image, but actually i just need to see how paths work so i can reach the .pdf files.
As a file path im getting this: resources\\uploads\\quality-docs\\FILENAME so i tried to reach them like this: http://localhost:3030/resources/uploads/quality-docs/FILENAME but it doesnt work, it gives me 404. Just realised when i put files in static, public folder, i can reach it like http://localhost:3030/FILENAME ... but is there a way for it to not be in public?
This is how my backend structure looks like:
Any ideas/sugestions are welcome, is this even a right way to go? Plus if any of you have idea how to delete files from server?
Assuming that you are using express in your node app, you need to include a static route to the resources/uploads directory (express static routes) like the following:
app.use(express.static('resources/uploads'))
For deleting files from a node application use unlink fs.unlink

Alternative to sendFile when linked files are not public

I'm doing a node project where I expose my public folder like:
app.use(express.static(path.join(__dirname, '/public')))
So now, all my public files are accessible through localhost:8080/*
I have also created a folder called "views" where I save private views, javascript and css files associated with them. They are private views so I don't want any user to access them.
As I have html linked with my css files and javascript, when the browser try to GET them, it says "not found" because they are not in the public folder.
I'm sending the html as sendFile in the express route.
Is there any way to put all files in the public folder and then protect them for not being accessible to public users? Or is there any alternative to sendFile, so the file is rendered locally and it doesn't need to request the css and javascript files
Thank you in advance
Views are technically private. Because they are rendered server-side, and not directly accessible by the visitors.
But you generally don't want stuff like Javascript, CSS nor views to be private. They will be seen anyway by the user. The only reason to have things like Node.js views private is the fact that they need be rendered by Node.js prior sending them to the user.
If you have private files, you might want to do same.
Otherwise, simply place them on the /public folder. You should not be hiding any secrets inside your JS / CSS code.
Edit (following comment):
You have a couple ways to do that.
Either you build a unique response that contains all necessary HTML / views - CSS - JS.
render('view.ejs', { css: 'body { color: blue }' })
You will need render that variable into your view, just like you might already be doing with your other views.
You might also want to read it from a file:
fs.readFile(`${__dirname}/css/style.css`, (error, styles) => { ... }
Or you handle each file request separately:
Node.js - external JS and CSS files (just using node.js not express)
(if you use Node.js views simply render these one instead of HTML files)

About folder structure in nodejs

Actually , I am beginner in nodejs field . So want to know where to place(folder name) what file (like html file , css file , js file other files).
What is the correct folder structure(formating) of the nodejs web application.
ie...
public folder is for (html , css , image etc)
I am confused. Can you help. And want to know what is the need of this type of folder convention.Is it is a standard or any other reason. ?
You can create to folder in your main project.
lib - this will hold all the server related logic.
public - this will have the client side related files. here you can create the sub folders named as : html, css and js. At the time of execution this folder you can serve as static files.
main
---lib
---public
--------html
--------css
--------js
server.js
config.json

Refreshing URL glitch - cloud updating/rewriting

Hello I've created an VBA script which saves me jpg from excel and then gsync uploads it on gDrive, but here comes the thing. The URL for downloading is volatile and I need full resolution image.
There is link so you can open in awful google UI
and I would like to open THIS => volatile link :(
Or can I use this VBA to upload image on some other Cloud directly from excel?
You can either use the Drive SDK to get the file details including the latest temporary download link:
https://developers.google.com/drive/v2/reference/files/get
...or you can make a parent or grandparent folder public and work out a URL direct to the image using its filename, like this:
http://gappstips.com/gmail/use-google-drive-to-host-your-gmail-signature/
http://drive.google.com/uc?export=view&id= "ID"
If you change URL in view mode you can view that image dirrect.
There are other files types.

Resources