repl.it
Express

A hassle free url shortener.

fork
loading
Files
  • index.js
  • static
  • Packager files
  • package-lock.json
  • package.json
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Express 🌐
const express = require('express');
const app = express();
app.use(express.static('static'))

// Database 🗃️
var jsonstore = require('jsonstore.io') 
let db = new jsonstore(process.env.KEY)

// Homepage 🏠
app.get('/', (req, res) => {
	res.sendFile("static/index.html", {root: __dirname});
});

// Shortener 👶
app.get('/shorten', (req, res) => {
	db.write(req.query.key, {"url": req.query.url});
	res.send("OK")
});

// Redirect 🌊
app.get('/:key', (req, res) => {
	db.read(req.params.key + "/url").then( (url) => {
		res.redirect(url);
	});
});

// Listener 👂
app.listen(3000, () => console.log('server started'));