1藕帜、為項(xiàng)目添加markdown-js依賴图谷,加入markdown解析能力:
express 并不直接支持markdown語(yǔ)法主之,需要為項(xiàng)目添加markdown-js模塊的依賴.
這里我們要修改package.json文件酵幕,添加相關(guān)依賴:
原package.json文件
{
"name": "blog",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "~1.15.2",
"cookie-parser": "~1.4.3",
"debug": "~2.2.0",
"ejs": "~2.5.2",
"express": "~4.14.0",
"morgan": "~1.7.0",
"serve-favicon": "~2.3.0"
}
}
修改成:
{
"name": "blog",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "~1.15.2",
"cookie-parser": "~1.4.3",
"debug": "~2.2.0",
"ejs": "~2.5.2",
"express": "~4.14.0",
"morgan": "~1.7.0",
"serve-favicon": "~2.3.0",
"markdown-js": ">= 0.0.1"
}
}
在最下面加入:"markdown-js": ">= 0.0.1"
name: 指定我們app的名稱,
dependesies: nodejs中定義依賴庫(kù),這里添加了對(duì)markdown-js的依賴缓艳,
private: 制定是否將程序發(fā)布到npm全局倉(cāng)庫(kù)中
使用npm安裝依賴:
$ npm install
安裝成功后,在 ./node_modules 目錄下會(huì)多出一個(gè) mardown-js 目錄
2校摩、讓Express支持Markdown:
打開index.js開始編寫node.js代碼, 在沒(méi)寫任何代碼前的index.js大致如下:
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
router.get('/fendo', function (req, res) {
res.send('hello,world!');
});
module.exports = router;
第一行導(dǎo)入 express 模塊阶淘, 可以理解成java中的import [packagename]
衙吩。一會(huì)需要用這個(gè)語(yǔ)法來(lái)導(dǎo)入markdown-js模塊模塊
在里面加入:
var markdown = require('markdown-js');
首先我們?cè)O(shè)計(jì)一個(gè)url
在index.js添加如下代碼:
router.get('/markdown', function(req, res) {
//markdown.markHtml(); 是將markdown格式的字符轉(zhuǎn)換成Html
var html = markdown.makeHtml("[fendo](http://blog.csdn.net/u011781521?viewmode=contents \"Click\") ");
res.send(html)
res.end();
})
訪問(wèn):http://localhost:3000/markdown
點(diǎn)擊fendo就進(jìn)入了我的博客。
3舶治、讓Express渲染markdown文件
在app.js中引入
var fs = require('fs');
var markdown = require('markdown-js');
然后給Express注冊(cè)Markdown渲染器
app.engine('md', function(path, options, fn){
fs.readFile(path, 'utf8', function(err, str){
if (err) return fn(err);
str = markdown.parse(str).toString();
fn(null, str);
});
});
完整的代碼:
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var index = require('./routes/index');
var users = require('./routes/users');
var fs = require('fs');
var markdown = require('markdown-js');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
app.engine('md', function(path, options, fn){
fs.readFile(path, 'utf8', function(err, str){
if (err) return fn(err);
str = markdown.parse(str).toString();
fn(null, str);
});
});
module.exports = app;
修改 "/markdown" url的處理程序
router.get('/markdown', function(req, res) {
res.render('index.md',{layout:false});
})
在views/目錄下創(chuàng)建一個(gè) index.md 文件
內(nèi)容如下
his is a demo page
===================
[fendo](http://blog.csdn.net/u011781521?viewmode=contents \"Click\")
重新運(yùn)行項(xiàng)目: npm start
訪問(wèn) http://localhost:3000/markdown
已經(jīng)成功構(gòu)建了第一個(gè)markdown頁(yè)面分井。