Skip to content
This repository has been archived by the owner on Jun 18, 2020. It is now read-only.

Serving PNG images #6

Open
prasanna-mswamy opened this issue Apr 23, 2019 · 9 comments
Open

Serving PNG images #6

prasanna-mswamy opened this issue Apr 23, 2019 · 9 comments

Comments

@prasanna-mswamy
Copy link

prasanna-mswamy commented Apr 23, 2019

Hi, I was trying to use this template to serve SPA files. I was following the article https://www.openfaas.com/blog/serverless-single-page-app/ . It works well except for images and I suspect for contents such as pdf. I tried modifying the template to use express' sendFile() and setting the right Content-Type and it works with that. However, I was not able to succeed sending files with either end() or pipe(). If there is an alternative without modifying the template, please let me know. Based on the suggestion, I can also make a pull request to correct the issue. Thanks in advance.

@alexellis
Copy link
Member

Please could you create a small github repo so that I can see how you were trying to send the file?

@prasanna-mswamy
Copy link
Author

Please find the issue replicated here, the code is almost same as in the article. https://github.com/traversals-analytics-and-intelligence/image-serve-issue-node10-express-template/tree/issue-showcase

But with sendFile() I'm able to serve image, as shown here, https://github.com/traversals-analytics-and-intelligence/image-serve-issue-node10-express-template/tree/hack-with-send-file

Please let me know your comments.

@prasanna-mswamy
Copy link
Author

@alexellis any advice?

@alexellis
Copy link
Member

I'm finding the examples quite confusing.

What is the goal? Just to serve static content without having any function or API?

Alex

@prasanna-mswamy
Copy link
Author

prasanna-mswamy commented Apr 26, 2019

In the example, yes. The goal is to have a function dedicated to serving only static content, similar to your article. I will try to explain my code better.

@prasanna-mswamy
Copy link
Author

prasanna-mswamy commented Apr 26, 2019

The handler provides the image as string here and that is sent in the index

Please refer the stackoverflow for context.

I have branched the code with a quick change to show when serving images work. When dealing with a file, if the handler returns only the path and then it is sent as a file in the handler here, then the image is successfully served.

When dealing with static files, the handler should provide on the path and sendFile() should be used to send them in the main (index).

Express also provides a builtin in static middleware.

Please let me know if I wasn't clear enough. Thanks.

@prasanna-mswamy
Copy link
Author

adding the line, app.use('/', express.static(path.join(__dirname, 'function/client'))); in index.js will serve files without any extra code required in the handler.js. If we can take the static path, set here as / from a configuration file, then it would add flexibility. @alexellis Please let me know what you think about this. Thanks.

@alexellis
Copy link
Member

I will take some time to look into this, but exposing express is not a goal of the function template.

Would you find it useful to have a template which just gave you the app from Express.js?

Alex

@prasanna-mswamy
Copy link
Author

Thanks. I understand that exposing express is not a goal of the template. If I get app from Express, then I would have more control. It would help.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants