question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

improve documentation about loading the public folder and read posts requests

See original GitHub issue

First of all a big thank you for Polka 💯 is always nice to have some diversity in the node ecosystem and this is really nice.

I think the documentation could use some improvements on how to load a /public folder. I appreciate that there is an example to use serve-static (https://github.com/lukeed/polka/blob/master/examples/with-serve-static/index.js) but I just can’t figure out how would you load e.g. a CSS file? I did try it and didn’t work for me.

So I did write this which I assume to be not the right way to do things but it works.

app.get('/public/:file', (req, res) => {

    console.log(req.params);
    if (fs.existsSync('./public/' + req.params.file)) {

        var file = fs.readFileSync('./public/' + req.params.file, 'utf8');
        res.write(file);
        res.end();

    } else {
        res.writeHead(200, {
            "Content-Type": "text/html; charset=utf-8"
        });
        res.write("⚽⚽⚽⚽ 404 - not found ⚽⚽⚽⚽");
        console.log(req);
        res.end('error')
        console.log(req.params);

    }

});

would serve-static provide a better approach to this ?

I believe that at least two elements have to be very clear in the readme/docs (without having to dig through all the example) a) how to parse post requests e.g. the current example just deal with JSON but I figured it out how to do it for forms too

app.use (bodyParser.urlencoded({ extended: true }));

b) how to load a public folder c) how to use templates (thanks to the good soul that provided polka-ejs!)

I understand that most people will use polka as a backend/API but why capture the rest of the audience too ? 😃 with EJS all is working very fine!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
kiennguyen1101commented, Feb 5, 2020

Does polka have built-in method to send html file? Something similar in express:

app.get("/contact", (req, res) => {
  return res.sendFile(path.join(__dirname, "../landing_page/contact.html"));
});
1reaction
gurugeekcommented, Oct 10, 2019

will do, much appreciated!! thanks again for your wonderful framework and libraries.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Public folders in Exchange Server - Microsoft Learn
Public folder content can include email messages, posts, documents, and eForms. The content is stored in the public folder mailbox but isn't ...
Read more >
Reading Files from public folder in play framework in production
The file parameter is usually dynamically extracted from the request path. Here is the typical mapping of the Assets controller in your conf/routes...
Read more >
Configure the public files folder - GitLab Docs
All the files that should be accessible by the browser must be in a root-level folder called public . Follow these instructions to...
Read more >
Managing Folders - Salesforce Help
A folder is a place where you can store reports, dashboards, documents, or email templates. Folders can be public, hidden, or shared, and...
Read more >
The File System Access API: simplifying access to local files
After a user grants a web app access, this API allows them to read or save changes directly to files and folders on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found