node10-express-template icon indicating copy to clipboard operation
node10-express-template copied to clipboard

Serving PNG images

Open prasanna-mswamy opened this issue 5 years ago • 9 comments

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.

prasanna-mswamy avatar Apr 23 '19 16:04 prasanna-mswamy

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

alexellis avatar Apr 23 '19 18:04 alexellis

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 avatar Apr 24 '19 10:04 prasanna-mswamy

@alexellis any advice?

prasanna-mswamy avatar Apr 26 '19 07:04 prasanna-mswamy

I'm finding the examples quite confusing.

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

Alex

alexellis avatar Apr 26 '19 07:04 alexellis

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 avatar Apr 26 '19 07:04 prasanna-mswamy

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 avatar Apr 26 '19 07:04 prasanna-mswamy

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.

prasanna-mswamy avatar May 07 '19 10:05 prasanna-mswamy

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

alexellis avatar May 07 '19 15:05 alexellis

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.

prasanna-mswamy avatar May 09 '19 06:05 prasanna-mswamy