Skip to content

Instantly share code, notes, and snippets.

@vbguard
Created August 25, 2019 09:32
Show Gist options
  • Select an option

  • Save vbguard/7fdfd6b1fba4941ba355cb26d7e80917 to your computer and use it in GitHub Desktop.

Select an option

Save vbguard/7fdfd6b1fba4941ba355cb26d7e80917 to your computer and use it in GitHub Desktop.
normalize request url get last pathname idx, qs, command
const url = require('url');
const path = require('path');
module.exports = function normalizeRequest(url) {
let idx;
const parsed = url.parse(uri, true);
const pathname = path.join(parsed.pathname, '/');
pathname = pathname.slice(1, pathname.length - 1);
idx = pathname.lastIndexOf('/');
idx = (idx < 0 ? pathname.length : idx);
return {
command: pathname.slice(0, idx).toLowerCase(),
id: pathname.slice(idx + 1),
query: parsed.query
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment