├── package.json ├── index.js └── .gitignore /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "url-shortener", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "nodemon ." 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "nanoid": "^3.1.28" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const http = require("http"); 2 | const { nanoid } = require("nanoid"); 3 | const url = require("url"); 4 | 5 | const server = http.createServer(); 6 | 7 | let dataStore = []; 8 | 9 | server.on("request", async (req, res) => { 10 | const parts = url.parse(req.url, true); 11 | if (parts.pathname === "/shorten") { 12 | const buffers = []; 13 | for await (const chunk of req) { 14 | buffers.push(chunk); 15 | } 16 | const data = Buffer.concat(buffers).toString(); 17 | const url = JSON.parse(data).url; 18 | const storedModel = dataStore.filter((model) => model.url == url); 19 | let urlModel; 20 | if (storedModel.length > 0) { 21 | urlModel = storedModel[0]; 22 | } else { 23 | urlModel = { 24 | url: url, 25 | shortenUrl: nanoid(), 26 | }; 27 | } 28 | dataStore.push(urlModel); 29 | res.writeHead(201, { "content-type": "application/json" }); 30 | res.end(JSON.stringify(urlModel)); 31 | } else if (parts.query.id) { 32 | let shortId = parts.query.id; 33 | const storedModel = dataStore.filter( 34 | (model) => model.shortenUrl == shortId 35 | ); 36 | if (storedModel.length > 0) { 37 | res.writeHead(302, { Location: storedModel[0].url }); 38 | res.end(); 39 | } else { 40 | res.writeHead(404); 41 | res.end("Url doesnt exist"); 42 | } 43 | } else { 44 | res.writeHead(401); 45 | res.end("Invalid path"); 46 | } 47 | }); 48 | 49 | server.on("error", (error) => { 50 | console.error(error.message); 51 | }); 52 | 53 | server.listen(3000); 54 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | .pnpm-debug.log* 9 | 10 | # Diagnostic reports (https://nodejs.org/api/report.html) 11 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 12 | 13 | # Runtime data 14 | pids 15 | *.pid 16 | *.seed 17 | *.pid.lock 18 | 19 | # Directory for instrumented libs generated by jscoverage/JSCover 20 | lib-cov 21 | 22 | # Coverage directory used by tools like istanbul 23 | coverage 24 | *.lcov 25 | 26 | # nyc test coverage 27 | .nyc_output 28 | 29 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 30 | .grunt 31 | 32 | # Bower dependency directory (https://bower.io/) 33 | bower_components 34 | 35 | # node-waf configuration 36 | .lock-wscript 37 | 38 | # Compiled binary addons (https://nodejs.org/api/addons.html) 39 | build/Release 40 | 41 | # Dependency directories 42 | node_modules/ 43 | jspm_packages/ 44 | 45 | # Snowpack dependency directory (https://snowpack.dev/) 46 | web_modules/ 47 | 48 | # TypeScript cache 49 | *.tsbuildinfo 50 | 51 | # Optional npm cache directory 52 | .npm 53 | 54 | # Optional eslint cache 55 | .eslintcache 56 | 57 | # Microbundle cache 58 | .rpt2_cache/ 59 | .rts2_cache_cjs/ 60 | .rts2_cache_es/ 61 | .rts2_cache_umd/ 62 | 63 | # Optional REPL history 64 | .node_repl_history 65 | 66 | # Output of 'npm pack' 67 | *.tgz 68 | 69 | # Yarn Integrity file 70 | .yarn-integrity 71 | 72 | # dotenv environment variables file 73 | .env 74 | .env.test 75 | .env.production 76 | 77 | # parcel-bundler cache (https://parceljs.org/) 78 | .cache 79 | .parcel-cache 80 | 81 | # Next.js build output 82 | .next 83 | out 84 | 85 | # Nuxt.js build / generate output 86 | .nuxt 87 | dist 88 | 89 | # Gatsby files 90 | .cache/ 91 | # Comment in the public line in if your project uses Gatsby and not Next.js 92 | # https://nextjs.org/blog/next-9-1#public-directory-support 93 | # public 94 | 95 | # vuepress build output 96 | .vuepress/dist 97 | 98 | # Serverless directories 99 | .serverless/ 100 | 101 | # FuseBox cache 102 | .fusebox/ 103 | 104 | # DynamoDB Local files 105 | .dynamodb/ 106 | 107 | # TernJS port file 108 | .tern-port 109 | 110 | # Stores VSCode versions used for testing VSCode extensions 111 | .vscode-test 112 | 113 | # yarn v2 114 | .yarn/cache 115 | .yarn/unplugged 116 | .yarn/build-state.yml 117 | .yarn/install-state.gz 118 | .pnp.* 119 | --------------------------------------------------------------------------------