Created
August 25, 2019 09:32
-
-
Save vbguard/7fdfd6b1fba4941ba355cb26d7e80917 to your computer and use it in GitHub Desktop.
normalize request url get last pathname idx, qs, command
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
| 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