MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/badcode/comments/p4eb9n/to_load_html_content_in_express_just_use_views/
r/badcode • u/BlackBearFTW • Aug 14 '21
25 comments sorted by
58
isn't there an express function sendfile
35 u/BlackBearFTW Aug 14 '21 I thought about using res.render("index.html'); instead of this nonsense 11 u/twoPoundsOfGoldfish Aug 14 '21 Aren't those supposed to be used for like dynamic templates? 9 u/BlackBearFTW Aug 14 '21 Yup, you should indeed use sendfile 2 u/BlackBearFTW Aug 14 '21 Yes 4 u/paulsmithkc 👨🏫 Aug 15 '21 Yeah.res.sendFile('public/index.html')
35
I thought about using res.render("index.html'); instead of this nonsense
11 u/twoPoundsOfGoldfish Aug 14 '21 Aren't those supposed to be used for like dynamic templates? 9 u/BlackBearFTW Aug 14 '21 Yup, you should indeed use sendfile
11
Aren't those supposed to be used for like dynamic templates?
9 u/BlackBearFTW Aug 14 '21 Yup, you should indeed use sendfile
9
Yup, you should indeed use sendfile
2
Yes
4
Yeah.
res.sendFile('public/index.html')
40
Image Transcription: Code
app.get("/", (req,res) => { fs.readFile("public/home.html", function(err,data){ res.writeHead(200, {"Content-Type": "text/html"}); res.write(data); res.end(); }); }); app.get("/terms", (req,res) => { fs.readFile("public/terms.html", function(err,data){ res.writeHead(200, {"Content-Type": "text/html"}); res.write(data); res.end(); }); });
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
17 u/jmd_akbar Aug 15 '21 Good human 😊
17
Good human 😊
36
Not being familiar enough with an api does not equate to bad code.
21
I've seen worse. Believe me. I found my code from 2 years ago when I started.
Well styled JS at least. They just don't grasp the fundamentals of Express.
Oh please. This is perfectly valid.
4 u/NF-MIP Aug 15 '21 Yes if res.sendFile and static serving doesn't exist in Express.
Yes if res.sendFile and static serving doesn't exist in Express.
-5
Imagine using express still
1 u/revicon Aug 15 '21 as opposed to? 1 u/Randolpho Aug 15 '21 Fastify? Adonis? Hapi? 2 u/throwaway1_x Aug 15 '21 Because they're shiny new frameworks? 1 u/Randolpho Aug 15 '21 Because they are better frameworks than express
1
as opposed to?
1 u/Randolpho Aug 15 '21 Fastify? Adonis? Hapi? 2 u/throwaway1_x Aug 15 '21 Because they're shiny new frameworks? 1 u/Randolpho Aug 15 '21 Because they are better frameworks than express
Fastify? Adonis? Hapi?
2 u/throwaway1_x Aug 15 '21 Because they're shiny new frameworks? 1 u/Randolpho Aug 15 '21 Because they are better frameworks than express
Because they're shiny new frameworks?
1 u/Randolpho Aug 15 '21 Because they are better frameworks than express
Because they are better frameworks than express
0
Finally a truly terrible piece of code
[deleted] — view deleted comment
35 u/MikeyBoonstra Aug 14 '21 You can just serve html as a static file in express. 12 u/BlackBearFTW Aug 14 '21 Or simply call a view engine and call req.render("page.html"); 20 u/DaMastaCoda Aug 14 '21 First: they aren't using a view engine. Second: it's cheaper to use res.sendFile 9 u/BlackBearFTW Aug 14 '21 True they could use .sendfile() without needing a view engine. 0 u/[deleted] Aug 14 '21 [deleted] — view deleted comment 13 u/TinyBreadBigMouth Aug 15 '21 As in they could doapp.get("/", (req, res) => res.sendFile("public/home.html")); app.get("/terms", (req, res) => res.sendFile("public/terms.html"));No need to manually load the file into memory and then write the HTTP response in parts.
You can just serve html as a static file in express.
12 u/BlackBearFTW Aug 14 '21 Or simply call a view engine and call req.render("page.html"); 20 u/DaMastaCoda Aug 14 '21 First: they aren't using a view engine. Second: it's cheaper to use res.sendFile 9 u/BlackBearFTW Aug 14 '21 True they could use .sendfile() without needing a view engine. 0 u/[deleted] Aug 14 '21 [deleted] — view deleted comment 13 u/TinyBreadBigMouth Aug 15 '21 As in they could doapp.get("/", (req, res) => res.sendFile("public/home.html")); app.get("/terms", (req, res) => res.sendFile("public/terms.html"));No need to manually load the file into memory and then write the HTTP response in parts.
12
Or simply call a view engine and call req.render("page.html");
20 u/DaMastaCoda Aug 14 '21 First: they aren't using a view engine. Second: it's cheaper to use res.sendFile 9 u/BlackBearFTW Aug 14 '21 True they could use .sendfile() without needing a view engine.
20
First: they aren't using a view engine. Second: it's cheaper to use res.sendFile
9 u/BlackBearFTW Aug 14 '21 True they could use .sendfile() without needing a view engine.
True they could use .sendfile() without needing a view engine.
13 u/TinyBreadBigMouth Aug 15 '21 As in they could doapp.get("/", (req, res) => res.sendFile("public/home.html")); app.get("/terms", (req, res) => res.sendFile("public/terms.html"));No need to manually load the file into memory and then write the HTTP response in parts.
13
As in they could do
app.get("/", (req, res) => res.sendFile("public/home.html")); app.get("/terms", (req, res) => res.sendFile("public/terms.html"));
No need to manually load the file into memory and then write the HTTP response in parts.
58
u/twoPoundsOfGoldfish Aug 14 '21
isn't there an express function sendfile
40
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Aug 14 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
36
u/AbuSumayah Aug 15 '21
Not being familiar enough with an api does not equate to bad code.
21
u/_newfap Aug 14 '21
I've seen worse. Believe me. I found my code from 2 years ago when I started.
2
u/PolishedCheese Aug 15 '21
Well styled JS at least. They just don't grasp the fundamentals of Express.
2
u/overtrick1978 Aug 15 '21
Oh please. This is perfectly valid.
-5
u/DotNetDeveloperDude Aug 15 '21
Imagine using express still
0
u/iiMoe Aug 15 '21
Finally a truly terrible piece of code
0
u/[deleted] Aug 14 '21
[deleted] — view deleted comment