nurl/app.js

94 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-08-05 20:14:17 +00:00
const express = require('express');
const app = express();
const path = require('path');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const config = require('./config');
const base58 = require('./base58.js');
const log4js = require('log4js');
const logger = log4js.getLogger();
logger.level = 'debug';
2016-02-07 21:16:07 +00:00
// grab the url model
2017-08-05 20:14:17 +00:00
const Url = require('./models/url');
2016-02-07 21:16:07 +00:00
mongoose.connect('mongodb://' + config.db.host + '/' + config.db.name);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
2017-08-05 20:14:17 +00:00
app.all('/*', function(req, res, next) {
// CORS headers
res.header("Access-Control-Allow-Origin", "*"); // restrict it to the required domain
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
// Set custom headers for CORS
res.header('Access-Control-Allow-Headers', 'Content-type,Accept,X-Access-Token,X-Key');
if (req.method == 'OPTIONS') {
res.status(200).end();
} else {
next();
}
});
2016-02-07 21:16:07 +00:00
app.get('/', function(req, res){
res.sendFile(path.join(__dirname, 'views/index.html'));
});
2017-08-05 20:14:17 +00:00
app.post('/api/v1/shorten', function(req, res){
const longUrl = req.body.url;
let shortUrl = '';
2016-02-07 21:16:07 +00:00
2017-08-05 20:14:17 +00:00
// check if url already exists in database
2016-02-07 21:16:07 +00:00
Url.findOne({long_url: longUrl}, function (err, doc){
2017-08-05 20:14:17 +00:00
2016-02-07 21:16:07 +00:00
if (doc){
shortUrl = config.webhost + base58.encode(doc._id);
// the document exists, so we return it without creating a new entry
res.send({'shortUrl': shortUrl});
} else {
// since it doesn't exist, let's go ahead and create it:
2017-08-05 20:14:17 +00:00
const newUrl = Url({
long_url: longUrl
});
2016-02-07 21:16:07 +00:00
2017-08-05 20:14:17 +00:00
// save the new link
2016-02-07 21:16:07 +00:00
newUrl.save(function(err) {
if (err){
2017-08-05 20:14:17 +00:00
logger.error(err);
2016-02-07 21:16:07 +00:00
}
shortUrl = config.webhost + base58.encode(newUrl._id);
res.send({'shortUrl': shortUrl});
});
}
});
});
app.get('/:encoded_id', function(req, res){
2017-08-05 20:14:17 +00:00
const base58Id = req.params.encoded_id;
2016-02-07 21:16:07 +00:00
2017-08-05 20:14:17 +00:00
const id = base58.decode(base58Id);
2016-02-07 21:16:07 +00:00
2017-08-05 20:14:17 +00:00
// check if url already exists in database
Url.findOneAndUpdate({_id: id}, {$inc:{visits:1}}, function (err, doc){
2016-02-07 21:16:07 +00:00
if (doc) {
2017-08-05 20:14:17 +00:00
logger.debug('doc', doc);
2016-02-07 21:16:07 +00:00
res.redirect(doc.long_url);
} else {
res.redirect(config.webhost);
}
});
});
2017-08-05 20:14:17 +00:00
const server = app.listen(3000, function () {
logger.info('Server listening on port 3000');
2016-02-07 21:16:07 +00:00
});