Skip to content
Snippets Groups Projects
Commit 1ad4f3d5 authored by BuildTools's avatar BuildTools
Browse files

init!

parents
No related branches found
No related tags found
No related merge requests found
/db/db.old
/node_modules/*
tdb.sql
cconf.json
# Shortnex
Open Source Shortener, Alpha.
Made with Express and nedb
# Setup
Basicly, you need node 9.
Do `npm i` for installing all packages.
Edit the conf.json for your stuff! IMPORTANT rename dont use tdb.sql use db.sql, tdb.sql is my test db.
Than `npm start` and your good to go!
# Executables?
Soon:TM:.
____ _ _
/ ___|| |__ ___ _ __| |_ _ __ _____ __
\___ \| '_ \ / _ \| '__| __| '_ \ / _ \ \/ /
___) | | | | (_) | | | |_| | | | __/> <
|____/|_| |_|\___/|_| \__|_| |_|\___/_/\_\
Shortnex v1.0
@author lordjbs
@requires nodejs9
\ No newline at end of file
{
"port":3001,
"save_ips":false,
"db": "tdb.db"
}
db.sql 0 → 100644
db/db.js 0 → 100644
var Datastore = require('nedb')
var db;
exports.load = () => {
db = new Datastore({ filename: require("../conf.json").db, autoload: true });
}
exports.insert = function(stuff, func) {
db.insert(stuff, function(err) {
func(err);
});
}
exports.get = function(stuff, func) {
db.find(stuff, function(err,docs) {
func(err,docs);
})
}
exports.db = db;
index.js 0 → 100644
/**
* ____ _ _
* / ___|| |__ ___ _ __| |_ _ __ _____ __
* \___ \| '_ \ / _ \| '__| __| '_ \ / _ \ \/ /
* ___) | | | | (_) | | | |_| | | | __/> <
* |____/|_| |_|\___/|_| \__|_| |_|\___/_/\_\
*
* Shortnex v1.0
* @author lordjbs
* @requires nodejs9
*/
const express = require('express');
const path = require('path');
const logger = require('morgan');
const bodyParser = require('body-parser');
const config = require("./conf.json");
const app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use('/', require("./routes/index.js"));
app.use('/shorten', require("./routes/shorten.js"));
app.use('/redirect', require("./routes/redirect.js"));
app.use(function(req, res, next) {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
app.use(function(err, req, res, next) {
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.status(err.status || 500);
return res.send({error:err.message});
});
const port = config.port || 3000;
app.listen(port, function () {
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream('ascii.txt')
});
lineReader.on('line', function (line) {
console.log(line);
});
setTimeout(function() {
console.log("Listening on Port: " + port)
console.log("Starting db...");
require("./db/db.js").load();
}, 1000);
});
module.exports = app;
This diff is collapsed.
{
"name": "shortnex",
"version": "1.0.0",
"description": "Shortnex | Easy URL shortener.",
"main": "index.js",
"dependencies": {
"body-parser": "^1.18.2",
"express": "^4.16.2",
"morgan": "^1.9.0",
"nedb": "^1.8.0",
"path": "^0.12.7",
"snekfetch": "^3.6.1"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/lordjbs/Shortenx.git"
},
"keywords": [
"shortener"
],
"author": "lordjbs",
"license": "ISC",
"bugs": {
"url": "https://github.com/lordjbs/Shortenx/issues"
},
"homepage": "https://github.com/lordjbs/Shortenx#readme"
}
<html>
<body>
</body>
</html>
var express = require('express');
var router = express.Router();
var path = require('path');
router.get('/', function(req, res, next) {
return res.sendFile(path.resolve('public/html/index.html'));
});
module.exports = router;
var express = require('express');
var router = express.Router();
var path = require('path');
router.get('/', function(req, res, next) {
const id = req.query.id;
console.log(id);
if(!id) {
return res.send({error:"params missing."});
}
try{
require("../db/db.js").get({id}, function(err,docs) {
var u = docs[0].url
if(!u.startsWith("https://") || !u.startsWith("http://")) {
u = "http://" + u;
}
res.redirect(docs[0].url);
});
}catch(e) {
return res.send({error});
}
});
// CMuqYSdQyJUi & M5mJlfojWuBd
module.exports = router;
var express = require('express');
var router = express.Router();
var path = require('path');
router.get('/', function(req, res, next) {
const url = req.query.url;
const ip = 0;
const createdat = new Date();
if(!url) {
return res.send({error:"params missing."});
}
try{
var id = makeid();
const db = require("../db/db.js");
db.insert({id, url, ip, createdat}, function(err) {
if(err) {
res.send(err);
}else {
res.send({success:true, id });
}
});
} catch(e) {
res.send({error:e});
console.error(e)
}
});
function makeid() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 12; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
module.exports = router;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment