Express

A hassle free url shortener.

fork
loading
Files
  • index.js
  • static
  • package-lock.json
  • package.json

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.405714775779066","path":"index.js","file":{"path":"index.js","content":{"asEncoding":{"base64":"Ly8gRXhwcmVzcyDwn4yQCmNvbnN0IGV4cHJlc3MgPSByZXF1aXJlKCdleHByZXNzJyk7CmNvbnN0IGFwcCA9IGV4cHJlc3MoKTsKYXBwLnVzZShleHByZXNzLnN0YXRpYygnc3RhdGljJykpCgovLyBEYXRhYmFzZSDwn5eD77iPCnZhciBqc29uc3RvcmUgPSByZXF1aXJlKCdqc29uc3RvcmUuaW8nKSAKbGV0IGRiID0gbmV3IGpzb25zdG9yZShwcm9jZXNzLmVudi5LRVkpCgovLyBIb21lcGFnZSDwn4+gCmFwcC5nZXQoJy8nLCAocmVxLCByZXMpID0+IHsKCXJlcy5zZW5kRmlsZSgic3RhdGljL2luZGV4Lmh0bWwiLCB7cm9vdDogX19kaXJuYW1lfSk7Cn0pOwoKLy8gU2hvcnRlbmVyIPCfkbYKYXBwLmdldCgnL3Nob3J0ZW4nLCAocmVxLCByZXMpID0+IHsKCWRiLndyaXRlKHJlcS5xdWVyeS5rZXksIHsidXJsIjogcmVxLnF1ZXJ5LnVybH0pOwoJcmVzLnNlbmQoIk9LIikKfSk7CgovLyBSZWRpcmVjdCDwn4yKCmFwcC5nZXQoJy86a2V5JywgKHJlcSwgcmVzKSA9PiB7CglkYi5yZWFkKHJlcS5wYXJhbXMua2V5ICsgIi91cmwiKS50aGVuKCAodXJsKSA9PiB7CgkJcmVzLnJlZGlyZWN0KHVybCk7Cgl9KTsKfSk7CgovLyBMaXN0ZW5lciDwn5GCCmFwcC5saXN0ZW4oMzAwMCwgKCkgPT4gY29uc29sZS5sb2coJ3NlcnZlciBzdGFydGVkJykpOw=="},"asBuffer":null},"loaded":true}}
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'));