在sexpress中添加了markdown支持,在render时,可以渲染*.md格式文件。
思路
1.将所有markdown文件(即:*.md文件),统一放到views/blogs目录下
2.将/blogs/*.html的url, 映射到markdown文件
3.在首页index.ejs中,添加文章的链接
1. 在index.js中添加url处理代码:
app.get('/blogs/:title.html', function(req, res, next) {
var path = [
'blogs/',
req.params.title, '.md'
].join('');
console.log(path)
res.render(path, {layout: false});
})
2. 在views/blogs文件夹中添加*.md文件:
views/blogs/fendo.md
# amdefine
A module that can be used to implement AMD's define() in Node. This allows you
to code to the AMD API and have the module work in node programs without
requiring those other programs to use AMD.
## Usage
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<h1><%= title %></h1>
<p>Welcome to <%= title %></p>
<ul>
<li><a href="/markdown">Markdown Demo page</a></li>
<li><a href="/blogs/fendo.html">My fendo markdown blog</a></li>
</ul>
</body>
</html>
在运行项目: npm start
点击: Markdown Demo page
点击: My fendo markdown blog
搞定!!
本文介绍了一种使用Node.js的Express框架来渲染Markdown文件的方法。通过简单的URL映射和视图设置,可以轻松地将Markdown文档转换为网页内容。
2950

被折叠的 条评论
为什么被折叠?



