Last active
December 14, 2015 16:29
-
-
Save clonn/5115290 to your computer and use it in GitHub Desktop.
node.js express, muliti-middleware method, node.js express,多層 middleware 使用方法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'), | |
routes = require('./routes'), | |
http = require('http'), | |
server; | |
var app = express(); | |
app.configure(function(){ | |
app.set('port', process.env.PORT || 3000); | |
app.set('views', __dirname + '/views'); | |
app.set('view engine', 'ejs'); | |
app.use(express.logger('dev')); | |
app.use(express.bodyParser()); | |
app.use(express.methodOverride()); | |
app.use(express.cookieParser('your secret here')); | |
app.use(express.session()); | |
app.use(app.router); | |
}); | |
app.get('/test', | |
routes.test, | |
routes.renderview | |
); | |
server = http.createServer(app); | |
server.listen(app.get('port'), function(){ | |
console.log("Express server listening on port " + app.get('port')); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<title><%= title %></title> | |
</head> | |
<body> | |
<h1><%= title %></h1> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
step = require('step'); | |
exports.test = function(req, res, next){ | |
test(req, res, next); | |
}; | |
exports.renderview = function(req, res, next){ | |
res.render('index', { | |
title: 'mulit middleware method' | |
}); | |
}; | |
function test (req, res, next) { | |
step( | |
function () { | |
console.log('outter step1'); | |
this(); | |
}, | |
function () { | |
console.log('outter step2'); | |
(function (req, res, next) { | |
console.log('run inner 1'); | |
next(); | |
})(req, res, this); | |
}, | |
function () { | |
console.log('outter step3'); | |
(function (req, res, next) { | |
console.log('run inner 2'); | |
next(); | |
})(req, res, this); | |
}, | |
function () { | |
console.log('outter final'); | |
(function (req, res, next) { | |
console.log('run inner 3'); | |
res.send({status: 'ok'}); | |
})(req, res, next); | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment