Minblog is a blogging system with less than 200 lines of code. Support administrator login, Markdown edit articles
Project address github.com/tans/minblo…
Example address: blog.minapp.xin
const fs = require("fs");
const path = require("path");
const Koa = require("koa");
const Router = require("koa-router");
const Render = require("koa-ejs");
const bodyParser = require("koa-bodyparser");
const marked = require("marked"); // use koa const app = new koa (); const router = new Router(); Render(app, { root: path.join(__dirname,"view"),
layout: "layout",
viewExt: "ejs"}); // Administrator login passwordlet token = "minblog"; // The article is saved in posts.jsonlet posts = JSON.parse(fs.readFileSync("./posts.json").toString()); The router / / front page. Get ("/", async ctx => {
ctx.state.posts = posts;
await ctx.render("list"); }); Router.get ("/posts/:postid", async ctx => {
ctx.state.post = posts[ctx.params.postid];
ctx.state.postid = ctx.params.postid;
ctx.state.html = marked(ctx.state.post.content);
await ctx.render("detail"); }); Router.get ("/login", async ctx => {
await ctx.render("login"); }); Router.post ("/login", async ctx => {
if (ctx.request.body.token == token) {
ctx.cookies.set("token", token);
ctx.redirect("/");
}
ctx.body = "Wrong password"; }); // Post edit page router.get("/edit/:postid", async ctx => {
ctx.state.post = posts[ctx.params.postid] || {};
ctx.state.postid = ctx.params.postid;
await ctx.render("edit"); }); // Save the article router.post("/posts/:postid", async ctx => {
if(! ctx.state.logined) { ctx.body ="Not logged in";
}
if(ctx.request.body.show ! = ="true") {
delete ctx.request.body.show;
}
if (ctx.params.postid == "new") {
posts.push(ctx.request.body);
} else {
posts[ctx.params.postid] = ctx.request.body;
}
fs.writeFileSync("./posts.json", JSON.stringify(posts, null, 2));
ctx.redirect("/posts/"+ ctx.params.postid); }); App.use (async (CTX, next) => {ctx.state.logined =false;
if (ctx.cookies.get("token") == token) {
ctx.state.logined = true;
}
await next();
});
app.use(bodyParser());
app.use(router.routes()).use(router.allowedMethods());
app.listen(3000);
Copy the code
The front page
//layout.ejs <! DOCTYPE html> <html> <head> <title>minblog</title> <styletype="text/css">
body{
color:# 555;
padding:10px;
padding-top:40px;
max-width: 720px;
margin: 0 auto;
position: relative;
}
a{
color: # 001234;
font-weight: bolder;
text-decoration: none;
}
</style>
</head>
<body>
<a href="/" style="position: absolute; top:10px; left: 10px;">minblog</a>
<hr style="border:none; border-top:1px solid #eee"/> <% -body %> </body> </ HTML > // home section <styletype="text/css"> .articleList{list-style: none; padding: 0 } .articleList li{padding-top: 10px; } .articleList .hide{text-decoration: line-through; } </style> <div style="position:absolute; top:10px; right:10px;"> < %if(logined){%>
<a href="edit/new"> Publish </a> <%}else{%>
<a href="login"</a> <%}%> </div> <ul class="articleList"> < %for(var i = 0; i < posts.length; i++) { var postid = posts.length-i-1; % > < %if(posts[postid].show || logined){%>
<li class="<%=posts[postid].show? '':'hide'%>" >
<a href="posts/<%=postid%>"> < % = posts [postid]. Title % > < / a > < / li > < %} % > < %} % > < / ul > section < / / the article page h1 style ="text-align: center"><%=post.title%></h1>
<div><%-html%></div>
<div style="position:absolute; top:10px; right:10px;">
<a href=".. /edit/<%=postid%>"> edit < / a > < / div > / part/landing page < h2 > administrator login < / h2 > < form method ="post" action="login">
<input name="token" placeholder="Enter your login password"/> <button> Determine </button> </form> // edit the page section <form method="post" action=".. /posts/<%=postid%>"> <div> <div> title </div> <input style="width: 320px;" type="" name="title" value="<%=post.title%>"> < / div > < br / > < div > < div > content < / div > < textarea name ="content" style="width: 480px;" rows="30"> < % = post. The content % > < / textarea > < / div > < div > < div > is < / div > < select name ="show">
<option value=true <%=post.show?'selected':' '%> > Displays </option> <option value=false< % =! post.show?'selected':' '% > > hidden < option > < / select > < / div > < br / > < br / > < button > sure < / button > < / form >Copy the code