openfaas-incubator / node10-express-template

Node.js 10 Express Template for OpenFaaS
MIT License
24 stars 26 forks source link

Serving PNG images #6

Open prasanna-mswamy opened 5 years ago

prasanna-mswamy commented 5 years ago

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 commented 5 years ago

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

prasanna-mswamy commented 5 years ago

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 commented 5 years ago

@alexellis any advice?

alexellis commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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.