Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup Handlebars #26

Merged
merged 1 commit into from Nov 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
141 changes: 141 additions & 0 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions app/package.json
Expand Up @@ -10,7 +10,9 @@
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"express-handlebars": "^3.1.0",
"handlebars": "^4.5.2",
"handlebars-layouts": "^3.1.4",
"mongoose": "^5.7.11",
"snoowrap": "^1.20.1"
}
Expand Down
7 changes: 0 additions & 7 deletions app/public/cool.html

This file was deleted.

7 changes: 0 additions & 7 deletions app/public/index.html

This file was deleted.

2 changes: 1 addition & 1 deletion app/scrapers/redditScraper.js
Expand Up @@ -11,7 +11,7 @@ async function scrapeUser(authorizationCode) {
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
// TODO set this (it must mach the URL provided for the app on the Reddit developer portal)
redirectUri: "http://localhost:3000/connectAccounts.html",
redirectUri: "http://localhost:3000/connectAccounts",
});

const user = wrapper.getMe();
Expand Down
27 changes: 27 additions & 0 deletions app/server.js
@@ -1,5 +1,9 @@
const fs = require("fs");
const express = require("express");
const mongoose = require("mongoose");
const handlebars = require("handlebars");
const expressHandlebars = require("express-handlebars");
const layouts = require("handlebars-layouts");
const redditScraper = require("./scrapers/redditScraper");

//url param indicates machine mongodb is running on /nameOfDatabase
Expand All @@ -21,6 +25,16 @@ const User = mongoose.model("User", userSchema);

const app = express();

// Setup Handlebars and addons.
layouts.register(handlebars);
const viewsPath = __dirname + "/views";
app.engine("handlebars", expressHandlebars({
defaultLayout: null
}));
app.set("view engine", "handlebars");
app.set("views", viewsPath);
handlebars.registerPartial("layout", fs.readFileSync(viewsPath + "/layout.handlebars", "utf8"));

app.get("/create", (req,res) => {
//for testing let's create a user instance
let testUser = new User({
Expand All @@ -39,6 +53,19 @@ app.get("/analyzeData", async (req, res) => {
res.status(200).send(scrapedData);
});

app.get("/", (req, res) => {
res.status(200).render("index", {});
});

// TODO remove this test page.
app.get("/cool", (req, res) => {
res.status(200).render("cool", {});
});

app.get("/connectAccounts", (req, res) => {
res.status(200).render("connectAccounts", {});
});

//serve static files from public dir
app.use(express.static("public"));
app.listen(3000);
@@ -1,9 +1,8 @@
<html>
<title>Digital Mirror - Link Accounts</title>
<head>
{{#extend "layout"}}
{{#content "head"}}
<script src="connectAccounts.js"></script>
</head>
<body>
{{/content}}
{{#content "body"}}
<h1>Connect Reddit account</h1>
<div>
<!-- The href attribute will be set via JavaScript. -->
Expand All @@ -13,5 +12,5 @@ <h1>Connect Reddit account</h1>
<!-- The href attribute will be set via JavaScript. -->
<a id="analyzeLink">Analyze my data</a>
</div>
</body>
</html>
{{/content}}
{{/extend}}
6 changes: 6 additions & 0 deletions app/views/cool.handlebars
@@ -0,0 +1,6 @@
{{#extend "layout"}}
{{#content "body"}}
<h1>Cool cool cool</h1>
<a href='/'>home</a>
{{/content}}
{{/extend}}
6 changes: 6 additions & 0 deletions app/views/index.handlebars
@@ -0,0 +1,6 @@
{{#extend "layout"}}
{{#content "body"}}
<h1>Hello, big guy!</h1>
<a href='/cool'>C O O L</a>
{{/content}}
{{/extend}}
9 changes: 9 additions & 0 deletions app/views/layout.handlebars
@@ -0,0 +1,9 @@
<html>
<title>Digital Mirror</title>
<head>
{{{block "head"}}}
</head>
<body>
{{{block "body"}}}
</body>
</html>