Creating a blog application with Marko.js involves setting up routes for viewing blog posts, managing posts in a database, and rendering blog post content dynamically. Here's a step-by-step guide on how to create a basic blog application with Marko.js:
1. Setup your Project
Create a new project directory and initialize a new Node.js project:
mkdir marko-blog
cd marko-blog
npm init -y
2. Install Dependencies
Install the necessary packages:
npm install marko express @marko/express body-parser nedb slugify
marko
: The Marko.js library.
express
: The Express.js framework for creating the server.
@marko/express
: Middleware for using Marko.js with Express.
body-parser
: Middleware to parse incoming request bodies.
nedb
: Embedded database for storing blog posts.
slugify
: Library for creating URL-friendly slugs.
3. Project Structure
Organize your project directory like this:
marko-blog/
├── src/
│ ├── components/
│ ├── pages/
│ │ ├── index.marko
│ │ ├── blog/
│ │ │ ├── index.marko
│ │ │ └── post.marko
│ ├── layouts/
│ │ ├── main.marko
│ ├── server.js
├── package.json
4. Create Page Templates
Create the main layout template in src/layouts/main.marko
:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${input.title}</title>
</head>
<body>
<header>
<h1>${input.title}</h1>
</header>
<main>
<${input.body} />
</main>
</body>
</html>
Create the homepage template in src/pages/index.marko
:
<layout-use template="../../layouts/main.marko">
<layout-put into="title">My Blog</layout-put>
<layout-put into="body">
<h2>Recent Posts</h2>
<ul>
<for|post| of=input.posts>
<li><a href="/blog/${post.slug}">${post.title}</a></li>
</for>
</ul>
</layout-put>
</layout-use>
Create the blog post template in src/pages/blog/post.marko
:
<layout-use template="../../layouts/main.marko">
<layout-put into="title">${input.post.title}</layout-put>
<layout-put into="body">
<article>
<h2>${input.post.title}</h2>
<p>${input.post.content}</p>
</article>
</layout-put>
</layout-use>
5. Setup the Express Server
Create src/server.js
to set up the Express server and define routes:
const express = require('express');
const markoMiddleware = require('@marko/express').default;
const bodyParser = require('body-parser');
const slugify = require('slugify');
const Datastore = require('nedb');
const app = express();
const db = new Datastore({ filename: './data/posts.db', autoload: true });
app.use(markoMiddleware());
app.use(bodyParser.urlencoded({ extended: true }));
// Homepage
app.get('/', (req, res) => {
db.find({}, (err, posts) => {
if (err) {
res.status(500).send('Error fetching posts');
} else {
res.marko(require('./pages/index.marko'), { posts });
}
});
});
// Blog post page
app.get('/blog/:slug', (req, res) => {
const { slug } = req.params;
db.findOne({ slug }, (err, post) => {
if (err || !post) {
res.status(404).send('Post not found');
} else {
res.marko(require('./pages/blog/post.marko'), { post });
}
});
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
6. Create Blog Posts
Create blog posts by inserting them into the database. You can do this manually or create an admin interface to add posts dynamically.
7. Run Your Application
Start your application by running:
node src/server.js
Now, open your browser and navigate to http://localhost:3000
to see the list of recent posts. Click on a post to view its content.
This setup provides a basic blog application with Marko.js and Express.js. You can expand this application by adding features such as pagination, commenting, user authentication, and an admin interface for managing posts. Marko's component-based architecture makes it easy to create dynamic and interactive user interfaces for your blog.