Move to a node server with handlebars templates

This commit is contained in:
Robbie Antenesse 2019-09-05 17:20:24 -06:00
parent f04d8182b6
commit 809f07504d
16 changed files with 885 additions and 4294 deletions

5
.gitignore vendored
View File

@ -1,3 +1,4 @@
node_modules/ node_modules/
dist/ public/
.cache/
**/*.log

View File

@ -7,24 +7,26 @@
"author": "Robbie Antenesse <dev@alamantus.com>", "author": "Robbie Antenesse <dev@alamantus.com>",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"start": "npm run watch-js", "start": "npm run compile-sass && npm run start-server",
"watch-js": "parcel watch src/index.html --no-hmr", "start-server": "node server.js",
"serve-js": "parcel src/index.html", "compile-sass": "node process-styles.js"
"build": "parcel build src/index.html --no-source-maps",
"clear": "npm run clear-dist && npm run clear-cache",
"clear-dist": "rimraf dist/{*,.*}",
"clear-cache": "rimraf .cache/{*,.*}"
}, },
"devDependencies": { "devDependencies": {
"autoprefixer": "^9.6.1", "autoprefixer": "^9.6.1",
"choo-devtools": "^3.0.1", "cssnano": "^4.1.10",
"parcel-bundler": "^1.12.3",
"parcel-plugin-goodie-bag": "^2.0.0",
"rimraf": "^3.0.0",
"sass": "^1.23.0-module.beta.1" "sass": "^1.23.0-module.beta.1"
}, },
"dependencies": { "dependencies": {
"choo": "^7.0.0", "fastify": "^2.8.0",
"picnic": "^6.5.1" "fastify-caching": "^5.0.0",
"fastify-compress": "^0.11.0",
"fastify-formbody": "^3.1.0",
"fastify-helmet": "^3.0.1",
"fastify-static": "^2.5.0",
"handlebars": "^4.2.0",
"html-minifier": "^4.0.0",
"make-promises-safe": "^5.0.0",
"picnic": "^6.5.1",
"point-of-view": "^3.5.0"
} }
} }

34
process-styles.js Normal file
View File

@ -0,0 +1,34 @@
const sass = require('sass');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
const postcss = require('postcss');
const fs = require('fs');
sass.render({ file: 'index.scss' }, function (sassErr, css) {
if (sassErr) {
throw sassErr;
}
// fs.writeFile('public/css/index.css', css.css, (fileErr) => {
// if (fileErr) {
// throw fileErr;
// }
// console.log('The file has been saved!');
// });
postcss([autoprefixer,cssnano]).process(css.css, {from:undefined}).then(result => {
result.warnings().forEach(warn => {
console.warn(warn.toString());
});
fs.writeFile('public/css/index.css', result.css, (fileErr) => {
if (fileErr) {
throw fileErr;
}
console.log('The file has been saved!');
});
}).catch((postcssErr) => {
throw postcssErr;
});
});

16
routes/home.js Normal file
View File

@ -0,0 +1,16 @@
async function routes(fastify, options) {
fastify.get('/', async (request, reply) => {
// return { hello: 'world' }
reply.view('home.hbs', { text: 'test' });
});
// fastify.get('/search/:id', async function (request, reply) {
// const result = await collection.findOne({ id: request.params.id })
// if (result.value === null) {
// throw new Error('Invalid value')
// }
// return result.value
// })
}
module.exports = routes

15
routes/resources.js Normal file
View File

@ -0,0 +1,15 @@
async function routes(fastify, options) {
fastify.get('/styles/:css', async (request, reply) => {
reply.sendFile('css/' + request.params.css);
});
// fastify.get('/search/:id', async function (request, reply) {
// const result = await collection.findOne({ id: request.params.id })
// if (result.value === null) {
// throw new Error('Invalid value')
// }
// return result.value
// })
}
module.exports = routes

50
server.js Normal file
View File

@ -0,0 +1,50 @@
'use strict'
require('make-promises-safe'); // installs an 'unhandledRejection' handler
const path = require('path');
const fastify = require('fastify')({
logger: true,
});
fastify.register(require('fastify-helmet'));
fastify.register(require('fastify-compress'));
fastify.register(require('fastify-formbody'));
fastify.register(require('fastify-static'), {
root: path.join(__dirname, 'public'),
});
fastify.register(require('point-of-view'), {
engine: {
handlebars: require('handlebars'),
},
templates: 'views',
options: {
useHtmlMinifier: require('html-minifier'),
htmlMinifierOptions: {
removeComments: true,
removeCommentsFromCDATA: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: true,
removeEmptyAttributes: true
},
partials: {
head: 'partials/head.hbs',
header: 'partials/header.hbs',
foot: 'partials/foot.hbs',
footer: 'partials/footer.hbs',
}
},
});
// Routes
fastify.register(require('./routes/resources'));
fastify.register(require('./routes/home'));
// Start the server
fastify.listen(3000, function (err, address) {
if (err) {
fastify.log.error(err);
process.exit(1);
}
fastify.log.info(`server listening on ${address}`);
});

28
views/home.hbs Normal file
View File

@ -0,0 +1,28 @@
{{> header }}
<section>
<h2 class="subtitle">An attempt at a viable alternative to Goodreads</h2>
<article class="flex two">
<div class="half">
<div class="card">
<header>
<p>Still gotta figure out a design.</p>
</header>
</div>
</div>
<div class="half">
<div class="card">
<header>
<p>It's early days, my friends!</p>
</header>
</div>
</div>
</article>
<article class="test">
{{ text }}
</article>
</section>
{{> footer }}

7
views/partials/foot.hbs Normal file
View File

@ -0,0 +1,7 @@
<script>
(function() {
alert('test');
})();
</script>
</body>
</html>

View File

@ -0,0 +1,7 @@
</main>
<footer>
<p>test footer</p>
</footer>
{{> foot }}

15
views/partials/head.hbs Normal file
View File

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{ pageTitle }}</title>
<meta name="description" content="An attempt at a viable alternative to Goodreads">
<meta name="keywords" content="books, tracking, lists, bookshelves, bookshelf, rating, reviews, reading">
<link rel="stylesheet" href="/styles/index.css">{{!-- This is controlled by the resources router. --}}
<script src="index.js"></script>
</head>
<body>

25
views/partials/header.hbs Normal file
View File

@ -0,0 +1,25 @@
{{> head }}
<header>
<nav>
<div class="brand">
<a href="./">
<h1>Unnamed Book Tracker</h1>
</a>
</div>
<!-- responsive-->
<input id="navMenu" type="checkbox" class="show">
<label for="navMenu" class="burger pseudo button">&#8801;</label>
<div class="menu">
<label style="display: inline-block;">
<input type="text" id="headerSearchBar" placeholder="Search">
</label>
<a href="https://gitlab.com/Alamantus/book-tracker" class="pseudo button">Repo</a>
<a href="https://gitter.im/book-tracker/general" class="pseudo button">Chat</a>
</div>
</nav>
</header>
<main class="container">

4949
yarn.lock

File diff suppressed because it is too large Load Diff