0

How to write a function to shorten a URL using JavaScript?

26th Jul 2018, 11:54 AM
MUHAMMAD SHAHID RAFI C P
MUHAMMAD SHAHID RAFI C P - avatar
7 ответов
+ 2
You need server script eg. PHP for url redirection.
26th Jul 2018, 12:13 PM
Calviղ
Calviղ - avatar
+ 1
Front-end JavaScript cannot run from server side, it run from device browsers. Unless you refer to Node.js.
26th Jul 2018, 12:49 PM
Calviղ
Calviղ - avatar
+ 1
Assume you use Nodejs with Express and MongoDb database with modal UrlShorten. The part to shortening url could be: const mongoose = require("mongoose"); const UrlShorten = mongoose.model("UrlShorten"); module.exports = app => { //GET API for redirecting to Original URL app.get("/api/item/:code", async (req, res) => { }); //POST API for creating short url from Original URL app.post("/api/item", async (req, res) => { }); }; More detail information please visit https://codeburst.io/creating-custom-url-shortener-with-nodejs-de10bbbb89c7
26th Jul 2018, 1:24 PM
Calviղ
Calviղ - avatar
0
JavaScript function need
26th Jul 2018, 12:20 PM
MUHAMMAD SHAHID RAFI C P
MUHAMMAD SHAHID RAFI C P - avatar
0
Javascript cannot perform server function.
26th Jul 2018, 12:22 PM
Calviղ
Calviղ - avatar
0
but JavaScript can use for server side also
26th Jul 2018, 12:39 PM
MUHAMMAD SHAHID RAFI C P
MUHAMMAD SHAHID RAFI C P - avatar
0
assume refer to Nodejs then what about function
26th Jul 2018, 12:52 PM
MUHAMMAD SHAHID RAFI C P
MUHAMMAD SHAHID RAFI C P - avatar