a Custom url shortner that detects device type an customize it's response based on it, Built with Nodejs.
git clone https://github.com/m0bad/node-url-shortner.git
cd node-url-shortner
npm install
touch .env
SECRET_KEY=<any string>
DB_CONNECTION_URL=<mongo db url>
PORT = <port number>
node app.js
most routes require the user to be logged in and authorized.
to disable this , you will need to edit /routes/url.js
- open the url routes file
- remove the middleware from the route you want to disable
- save the file, and restart the server
turn router.get("/", isLoggedIn, listShortlinks);
To router.get("/", listShortlinks);
if you didn't remove the Auth middlewares , then every request to a route that requires the user login should hava a header Called Authorization
this is how it looks 'Authorization:Bearer {userToken}'
the user gets the userToken at signin/up
-
POST /auth/signup
Request Body- email [required]
- username [required]
- password [required]
-
POST /auth/signin
Request Body- email [required]
- password [required]
-
GET /shortlinks/:{username}
returns all the user shortlinks , returns empty array if none exists -
POST /shortlinks/:{username} creates a new shortlink and attach it with user creating it
Request Body:- slug [optional] short string to replace the long url, if it's not given, one is generated in the API
- web [required] Working Url
- android_primary [required] Working Url
- android_fallback [required] Working Url
- ios_primary [required] Working Url
- ios_fallback [required] Working Url
**example request by httpie: **
http post http://localhost:port/shortlinks/username web=www.example.com android_primary=www.example.com android_fallback=www.example.com ios_primary=www.example.com ios_fallback=www.example.com
Note that you will need to add the Authorization header to the request if you didn't remove the Auth middlewares
-
PUT /shortlinks/username/:{slug}
update an existing shortlink, Only sent attr will be updated, other will stay as is.Request Body:
- web [optional] Working Url
- android_primary [optional] Working Url
- android_fallback [optional] Working Url
- ios_primary [optional] Working Url
- ios_fallback [optional] Working Url
-
DELETE /shortlinks/username/:{slug}
-
GET /:{slug}
redirect to the original Url link (Desktop, Android, iphone) based on the request Device.