Skip to content

Instantly share code, notes, and snippets.

@leslie-alldridge
Created March 27, 2019 08:40
Show Gist options
  • Save leslie-alldridge/75ab72ecacc1c1c2efe2a5a4d79feb1f to your computer and use it in GitHub Desktop.
Save leslie-alldridge/75ab72ecacc1c1c2efe2a5a4d79feb1f to your computer and use it in GitHub Desktop.
server js for exercise
const express = require("express");
const hbs = require("express-handlebars");
var fs = require("fs");
const server = express();
// Middleware
server.engine(
"hbs",
hbs({
defaultLayout: "main",
extname: "hbs"
})
);
server.set("view engine", "hbs");
server.use(express.static("public"));
server.use(express.urlencoded({ extended: false }));
server.get("/", function(req, res) {
fs.readFile("./invoice.json", "utf8", function(err, data) {
if (err) {
return res.status(500).send("An Error Occured!");
}
var invoices = JSON.parse(data);
console.log(invoices);
res.render("puppies/index", invoices);
});
});
module.exports = server;
@leslie-alldridge
Copy link
Author

index.hbs

<div class="container">
  {{#each puppies}}
  <div class="puppy-list">
    <a href="/puppies/{{id}}"><img class="img-circle" src="{{image}}" alt="{{name}}"></a>
  </div>
  {{/each}}

  <ul>
    {{#each Invoices}}
    <li>Invoice is {{InvoiceID}}</li>
    {{#if Warnings}}
    {{#each Warnings}}
    <p>The error for the above item was: {{Message}}</p>
    {{/each}}
    {{/if}}
    {{/each}}
  </ul>
</div>

@leslie-alldridge
Copy link
Author

invoice.json

{
  "Invoices": [
    {
      "InvoiceID": "5ceffde5-6786-4987-b03a-bf88d262c286",

      "StatusAttributeString": "OK"
    },
    {
      "InvoiceID": "5ceffde5-6786-4987-b03a-bf88d262c286",

      "StatusAttributeString": "OK"
    },
    {
      "InvoiceID": "5ceffde5-6786-4987-b03a-bf88d262c286",

      "StatusAttributeString": "WARNING",
      "Warnings": [
        {
          "Message": "Only AUTHORISED invoices may have SentToContact updated."
        }
      ]
    },
    {
      "InvoiceID": "5ceffde5-6786-4987-b03a-bf88d262c286",

      "StatusAttributeString": "ERROR",
      "ValidationErrors": [
        {
          "Description": "Invoice not of valid type for creation"
        }
      ]
    }
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment