feat: rendering article page

This commit is contained in:
Sergey Yarkov 2022-03-17 21:28:22 +03:00
parent 23f717e6cb
commit 5a9799f210
8 changed files with 66 additions and 11 deletions

View File

@ -21,7 +21,11 @@ export const indexPageHandler = async (req, res) => {
}); });
} catch (error) { } catch (error) {
console.error(error); console.error(error);
res.render('./views/500.hbs', { title: '500 | Internal server error ' }); res.render(
'./views/500.hbs',
{ title: '500 | Internal server error ' },
500
);
} }
}; };
@ -51,10 +55,17 @@ export const getArticleHandler = async (req, res) => {
return; return;
} }
res.json({ data: data.rows }); res.render('./views/article.hbs', {
title: data.rows[0].title,
article: data.rows[0],
});
} catch (error) { } catch (error) {
console.error(error); console.error(error);
res.render('./views/500.hbs', { title: '500 | Internal server error ' }); res.render(
'./views/500.hbs',
{ title: '500 | Internal server error ' },
500
);
} }
}; };

View File

@ -15,5 +15,5 @@ export const waitMiddleware = async (req, res) => {
}); });
}; };
await wait(50); await wait(10);
}; };

View File

@ -14,6 +14,6 @@ import {
*/ */
import { waitMiddleware } from '../middlewares.js'; import { waitMiddleware } from '../middlewares.js';
router.get('/', indexPageHandler); router.get('/', indexPageHandler).middleware([waitMiddleware]);
router.get('/articles/:id', getArticleHandler).middleware([waitMiddleware]); router.get('/articles/:id', getArticleHandler).middleware([waitMiddleware]);
router.post('/articles', createArticleHandler); router.post('/articles', createArticleHandler);

View File

@ -1,8 +1,12 @@
body { body {
font-family: monospace; font-family: monospace;
text-align: justify; text-align: justify;
margin: 75px; margin-left: auto;
max-width: 600px; margin-right: auto;
margin-top: 5rem;
max-width: 900px;
font-size: 16px;
padding: 15px;
} }
.app .app-heading { .app .app-heading {
@ -11,8 +15,23 @@ body {
align-items: center; align-items: center;
} }
.app .app-tag {
background-color: #000;
color: #fff;
padding: 10px;
font-weight: bold;
border-radius: 2px;
font-size: 18px;
user-select: none;
}
.app .article-heading {
display: block;
}
.app .app-body article { .app .app-body article {
margin-top: 2rem; margin-top: 2rem;
line-height: 1.3rem;
} }
.app .app-footer { .app .app-footer {
@ -31,9 +50,14 @@ body {
text-align: left; text-align: left;
} }
.app .app-footer span {
display: block;
margin-bottom: 1rem;
}
.app .app-footer img { .app .app-footer img {
width: 11rem; width: 11rem;
margin: 0 30px; margin: 10px 30px;
} }
.app .app-footer .nodejs-logo, .app .app-footer .nodejs-logo,

View File

@ -2,6 +2,6 @@
{{> head }} {{> head }}
<body> <body>
<h1>{{ title }}</h1> <h1>{{ title }}</h1>
<a href="/">return to home</a> <a href="/">&lt; return to home</a>
</body> </body>
</html> </html>

View File

@ -2,6 +2,6 @@
{{> head }} {{> head }}
<body> <body>
<h1>{{ title }}</h1> <h1>{{ title }}</h1>
<a href="/">return to home</a> <a href="/">&lt; return to home</a>
</body> </body>
</html> </html>

20
views/article.hbs Normal file
View File

@ -0,0 +1,20 @@
<html lang='en'>
{{> head }}
<body>
<main class='app'>
<a href="/">&lt; return to home</a>
<div class='app-heading article-heading'>
<h1>{{ article.title }}</h1>
<span>{{ article.created_at }}</span>
</div>
<div class="app-body">
<p>{{ article.description }}</p>
<hr />
<article>
<p>{{ article.body }}</p>
</article>
</div>
{{> footer }}
</main>
</body>
</html>

View File

@ -4,7 +4,7 @@
<main class='app'> <main class='app'>
<div class='app-heading'> <div class='app-heading'>
<h1>Articles (5)</h1> <h1>Articles (5)</h1>
<small>{{title}}</small> <span class="app-tag">{{title}}</span>
</div> </div>
<div class='app-description'> <div class='app-description'>
<p>This app showcases a website built with NodeJS. The site has a set of <p>This app showcases a website built with NodeJS. The site has a set of