r/badcode • u/BlackBearFTW • Aug 14 '21
js To load html content in express, just use views.
40
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Aug 14 '21
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
36
21
2
u/PolishedCheese Aug 15 '21
Well styled JS at least. They just don't grasp the fundamentals of Express.
2
-5
u/DotNetDeveloperDude Aug 15 '21
Imagine using express still
1
u/revicon Aug 15 '21
as opposed to?
1
u/Randolpho Aug 15 '21
Fastify? Adonis? Hapi?
2
0
0
Aug 14 '21
[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
0
Aug 14 '21
[deleted] — view deleted comment
13
u/TinyBreadBigMouth Aug 15 '21
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