app.all(WEINRE_RE, function(req, res) { var options = parseurl(req); if (options.pathname === '/weinre/client') { return res.redirect('client/' + (options.search || '')); } req.url = options.path.replace('/weinre', ''); handleWeinreReq(req, res); });
app.use(function(req, res, next) { var referer = req.headers.referer; var options = parseurl(req); var path = options.path; var index = path.indexOf('/whistle/');
var options = parseurl(req); req.headers[config.PLUGIN_HOOK_NAME_HEADER] = config.PLUGIN_HOOKS.UI; req.url = options.path.replace(result[0].slice(0, -1), '');
app.use(function (req, res, next) { var views = req.session.views; if (!views) { views = req.session.views = {} } // get the url pathname var pathname = parseurl(req).pathname; // count the views views[pathname] = (views[pathname] || 0) + 1; next() });
const requestHandler = (cluster, req, res) => { const { pathname } = parseurl(req); const [service, workerId, command = 'health'] = pathname .substr(1, pathname.length) .split('/'); if (service === 'monitor') { return requestDispatch.bind( null, cluster, res )({ workerId: parseInt(workerId, 10) || null, command }); } res.statusCode = 404; return res.end('This service does not exist.'); }
dispatch(req, res) { const parsedUrl = parseurl(req); req.ip = ip(req); req.pathname = parsedUrl.pathname; req.query = parse(parsedUrl.query); let stack; const route = this.fetch(req.method, parsedUrl.pathname); if (!route) { stack = [ this.notFound || (() => end(req, res, 404, `${req.method} ${parsedUrl.pathname} is undefined.`)) ]; } else { req.params = route.params; ({ stack } = route); } return this.step(req, res, this.t.concat(stack)); }
app.use(function(req, res, next) { var views = req.session.views; if ( !views ) { views = req.session.views = {}; } var pathname = parseurl(req).pathname; // parseurl 模块的就是对 url 模块的包装,只不过 parseurl 对 url 模块进行了优化,采用了 memoization 技术进行缓存 //var pathname = url.parse(req.url).pathname; console.log('req.url: ' + req.url + '; pathname: ' + pathname); views[pathname] = ( views[pathname] || 0 ) + 1; next(); });
// This is another example of middleware. //track the number of times a user has gone to specific page //another middleware app.use(function(req, res, next){ var views = req.session.views; // If no views initialize an empty array if(!views) { views = req.session.views = {}; } // Get the current path var pathname = parseurl(req).pathname; // Increment the value in the array using the path as the key views[pathname] = (views[pathname] || 0) + 1; //since this is the middleware, next() is needed because it will continue down the pipeline next(); });
const requestHandler = (cluster, req, res) => { const { pathname } = parseurl(req); const [service, workerId, command = 'health'] = pathname .substr(1, pathname.length) .split('/'); if (service === 'monitor') { return requestDispatch.bind( null, cluster, res )({ workerId: parseInt(workerId, 10) || null, command }); } res.statusCode = 404; return res.end('This service does not exist.'); }
dispatch(req, res) { const parsedUrl = parseurl(req); req.ip = ip(req); req.pathname = parsedUrl.pathname; req.query = parse(parsedUrl.query); let stack; const route = this.fetch(req.method, parsedUrl.pathname); if (!route) { stack = [ this.notFound || (() => end(req, res, 404, `${req.method} ${parsedUrl.pathname} is undefined.`)) ]; } else { req.params = route.params; ({ stack } = route); } return this.step(req, res, this.t.concat(stack)); }