improve documentation about loading the public folder and read posts requests
See original GitHub issueFirst 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:
- Created 4 years ago
- Comments:11 (5 by maintainers)
Top GitHub Comments
Does polka have built-in method to send html file? Something similar in express:
will do, much appreciated!! thanks again for your wonderful framework and libraries.