var connect = require( 'connect' );
var http = require( 'http' );
var app = connect();
// gzip/deflate outgoing responses
var compression = require( 'compression' );
app.use(compression());
// store session state in browser cookie
var cookieSession = require( 'cookie-session' );
app.use(cookieSession({
keys: [ 'secret1' , 'secret2' ]
}));
// parse urlencoded request bodies into req.body
var bodyParser = require( 'body-parser' );
app.use(bodyParser.urlencoded({extended: false }));
// respond to all requests
app.use( function (req, res){
res.end( 'Hello from Connect!\n' );
});
//create node.js http server and listen on port
http.createServer(app).listen(3000);
var app = connect() // 初始化
app.use( function (req, res, next) {
// do something
})
// http 服务器,使用
http.createServer(app).listen(3000);
function handle () {
function app(req, res, next) { app.handle(req, res, next)}
app.handle = function (req, res, next) {
console.log( this );
}
app.statck = [];
return app;
}
var app = handle();
app() // ==> { [Function: app] handle: [Function], stack: [] }
app.apply({}) // ==>{ [Function: app] handle: [Function], stack: [] }
var merge = require( 'utils-merge' );
module.exports = createServer;
var proto = {};
function createServer() {
// 函数对象,这个对象能调用,能加属性
function app(req, res, next){ app.handle(req, res, next); }
merge(app, proto); // ===等于调用 Object.assign
merge(app, EventEmitter.prototype); // === 等于调用 Object.assign
app.route = '/' ;
app.stack = [];
return app;
}
proto.handle = function handle(req, res, out) {
var index = 0;
var protohost = getProtohost(req.url) || '' ; //获得 http://www.baidu.com
var removed = '' ;
var slashAdded = false ;
var stack = this .stack;
// final function handler
var done = out || finalhandler(req, res, {
env: env,
onerror: logerror
}); // 接口 done(err);
// store the original URL
req.originalUrl = req.originalUrl || req.url;
function next(err) {
if (slashAdded) {
req.url = req.url.substr(1); // 除掉 / 之后的字符串
slashAdded = false ; // 已经拿掉
}
if (removed.length !== 0) {
req.url = protohost + removed + req.url.substr(protohost.length);
removed = '' ;
}
// next callback
var layer = stack[index++];
// all done
if (!layer) {
defer(done, err); // 没有中间件,调用 finalhandler 进行处理,如果 err 有值,就返回 404 进行处理
return ;
}
// route data
var path = parseUrl(req).pathname || '/' ;
var route = layer.route;
// skip this layer if the route doesn't match
if (path.toLowerCase().substr(0, route.length) !== route.toLowerCase()) {
return next(err); // 执行下一个
}
// skip if route match does not border "/", ".", or end
var c = path[route.length];
if (c !== undefined && '/ ' !== c && ' . ' !== c) {
return next(err); // 执行下一个
}
// trim off the part of the url that matches the route
if (route.length !== 0 && route !== ' / ') {
removed = route;
req.url = protohost + req.url.substr(protohost.length + removed.length);
// ensure leading slash
if (!protohost && req.url[0] !== ' / ') {
req.url = ' /' + req.url;
slashAdded = true ;
}
}
// call the layer handle
call(layer.handle, route, err, req, res, next);
}
next();
};
function call(handle, route, err, req, res, next) {
var arity = handle.length;
var error = err;
var hasError = Boolean(err);
debug( '%s %s : %s' , handle.name || '<anonymous>' , route, req.originalUrl);
try {
if (hasError && arity === 4) {
// error-handling middleware
handle(err, req, res, next);
return ;
} else if (!hasError && arity < 4) {
// request-handling middleware
handle(req, res, next);
return ;
}
} catch (e) {
// replace the error
error = e;
}
// continue
next(error);
}
proto.use = function use(route, fn) {
var handle = fn; // fn 只是一个函数的话 三种接口 // 1. err, req, res, next 2. req, res, 3, req, res, next
var path = route;
// default route to '/'
if ( typeof route !== 'string' ) {
handle = route;
path = '/' ;
}
// wrap sub-apps
if ( typeof handle.handle === 'function' ) { // 自定义中的函数对象
var server = handle;
server.route = path;
handle = function (req, res, next) { // req, res, next 中间件
server.handle(req, res, next);
};
}
// wrap vanilla http.Servers
if (handle instanceof http.Server) {
handle = handle.listeners( 'request' )[0]; // (req, res) // 最后的函数
}
// strip trailing slash
if (path[path.length - 1] === '/' ) {
path = path.slice(0, -1);
}
// add the middleware
debug( 'use %s %s' , path || '/' , handle.name || 'anonymous' );
this .stack.push({ route: path, handle: handle });
return this ;
};
/* istanbul ignore next */
var defer = typeof setImmediate === 'function'
? setImmediate
: function (fn){ process.nextTick(fn.bind.apply(fn, arguments)) }
function () {
var cb = Array.from(arguments)[0];
var args = Array.from(arguments).splice(1);
process.nextTick( function () {
cb.apply( null ,args);
})
}
function (..args){ process.nextTick(fn.bind(...args)) }
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有