Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created January 29, 2020 01:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prof3ssorSt3v3/26e14307c4a59bfa402268f4e1494a9a to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/26e14307c4a59bfa402268f4e1494a9a to your computer and use it in GitHub Desktop.
From Video tutorial about Express JS and the many types of variables and settings
"use strict";
const express = require("express");
const app = express();
const apples = require("./routes/apples");
const port = process.env.port || 4040;
app.set("views", "./views");
app.set("view engine", "pug");
app.set("custom", "Mandalorian");
app.locals.ihave = "spoken"; //lifetime of application
app.use("/apples", apples);
app.get("/", (req, res) => {
res.json({ cheeses: ["Cheddar", "Danish Blue", "Mozzarella"] });
});
app.get("/abc", (req, res) => {
res.render(
"index",
{ title: "Pugified", who: app.get("custom"), p: port },
(err, html) => {
if (err) {
res.status(500).end(err);
}
res.send(html);
}
);
});
app.listen(port, err => {
if (err) {
console.log("ERROR", err);
return;
}
console.log(`Listening on port ${port}`);
});
const express = require("express");
const router = express.Router();
router.use(function(req, res, next) {
req.newProp = "Baby Yoda rules!";
next();
});
router
.route("/fuji")
.get((req, res) => {
//handle gets for /apples/fuji
res.send("GET fuji " + req.newProp);
})
.post((req, res) => {
//handle posts for /apples/fuji
res.locals.myvar = "some value"; //lifetime of the request
res.send("POST fuji.");
});
router
.route("/empire")
.get((req, res) => {
//handle gets for /apples/empire
req.send("GET empire");
})
.post((req, res) => {
//handle posts for /apples/empire
req.send("POST empire");
});
module.exports = router;
doctype html
html
head
title #{title}
body
h1 #{title}
p Some text about #{who}
p #{settings.custom}
p I have #{ihave}
// app.locals.settings.custom
// app.locals.ihave
p Page generated for port #{p}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment