wip pre moving from bower to browserfy

This commit is contained in:
Martin Donnelly 2017-09-25 17:48:55 +01:00
parent d25b1a247a
commit 445a0c0411
5 changed files with 68 additions and 70 deletions

View File

@ -12,35 +12,44 @@
"es6": true
},
"rules": {
"no-new-object": 1,
"no-reserved-keys": 1,
"no-array-constructor": 1,
"quotes": [1, "single"],
"max-len": [1, 120, 2], // 2 spaces per tab, max 80 chars per line
"no-inner-declarations": [1, "both"],
"no-shadow-restricted-names": 1,
"one-var": 0,
"vars-on-top": 1,
"eqeqeq": 1,
"arrow-spacing": "error",
"block-scoped-var": "error",
"block-spacing": "error",
"brace-style": ["error", "stroustrup", {}],
"camelcase": "error",
"comma-dangle": ["error", "never"],
"comma-spacing": ["error", { "before": false, "after": true }],
"comma-style": [1, "last"],
"consistent-this": [1, "_this"],
"curly": [1, "multi"],
"eol-last": 1,
"eqeqeq": 1,
"func-names": 1,
"indent": ["error", 2, { "SwitchCase": 1 }],
"lines-around-comment": ["error", { "beforeBlockComment": true, "allowArrayStart": true }],
"max-len": [1, 120, 2], // 2 spaces per tab, max 80 chars per line
"new-cap": 1,
"newline-before-return": "error",
"no-array-constructor": 1,
"no-inner-declarations": [1, "both"],
"no-mixed-spaces-and-tabs": 1,
"no-multi-spaces": 2,
"no-new-object": 1,
"no-shadow-restricted-names": 1,
"object-curly-spacing": ["error", "always"],
"padded-blocks": ["error", { "blocks": "never", "switches": "always" }],
"prefer-const": "error",
"prefer-template": "error",
"one-var": 0,
"quote-props": ["error", "always"],
"quotes": [1, "single"],
"radix": 1,
"semi": [1, "always"],
"space-before-blocks": [1, "always"],
"space-infix-ops": 1,
"eol-last": 1,
"comma-style": [1, "last"],
"no-comma-dangle": 1,
"semi": [1, "always"],
"radix": 1,
"camelcase": 1,
"new-cap": 1,
"consistent-this": [1, "_this"],
"func-names": 1,
"no-multi-spaces": 2,
"brace-style": [2,"1tbs",{}],
"indent": [2,2],
"comma-spacing": ["error", { "before": false, "after": true }],
"object-curly-spacing": ["error", "always"]
"vars-on-top": 1,
"no-multiple-empty-lines": ["error", { "max": 1, "maxEOF": 1 }],
"spaced-comment": ["error", "always", { "markers": ["/"] }]
}
}

31
app.js
View File

@ -18,7 +18,7 @@ const Url = require('./models/url');
let isProduction = false;
let sitePath = 'public';
let indexView = 'views/index.html';
let listView = 'views/list.html';
const listView = 'views/list.html';
process.env.NODE_ENV = process.env.NODE_ENV || 'dev';
@ -31,14 +31,13 @@ if (process.env.NODE_ENV === 'prod') {
logger.warn(`isProduction:${isProduction}`);
mongoose.connect('mongodb://' + config.db.host + '/' + config.db.name);
mongoose.connect(`mongodb://${ config.db.host }/${ config.db.name}`);
app.use(compression());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.urlencoded({ 'extended': true }));
app.use(express.static(path.join(__dirname, sitePath)));
app.use(favicon(__dirname + '/live/favicon-16x16.png'));
app.use(favicon(`${__dirname }/live/favicon-16x16.png`));
app.all('/*', (req, res, next) => {
// CORS headers
@ -50,7 +49,6 @@ app.all('/*', (req, res, next) => {
res.status(200).end();
else
next();
});
app.get('/', function(req, res) {
@ -66,17 +64,17 @@ function postShort(req, res) {
let shortUrl = '';
// check if url already exists in database
Url.findOne({ long_url: longUrl }, (err, doc) => {
Url.findOne({ 'long_url': longUrl }, (err, doc) => {
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 {
}
else {
// since it doesn't exist, let's go ahead and create it:
const newUrl = Url({
long_url: longUrl
'long_url': longUrl
});
// save the new link
@ -84,15 +82,12 @@ function postShort(req, res) {
if (err)
logger.error(err);
shortUrl = config.webhost + base58.encode(newUrl._id);
res.send({ 'shortUrl': shortUrl });
});
}
});
}
function getList(req, res) {
@ -103,28 +98,24 @@ function getList(req, res) {
}
function getEncodedID(req, res) {
const base58Id = req.params.encoded_id;
const id = base58.decode(base58Id);
// check if url already exists in database
Url.findOneAndUpdate({ _id: id }, { $inc: { visits: 1 } }, (err, doc) => {
Url.findOneAndUpdate({ '_id': id }, { '$inc': { 'visits': 1 } }, (err, doc) => {
if (doc) {
logger.debug(`Redirect: ${doc.long_url}`);
res.redirect(doc.long_url);
} else
}
else
res.redirect(config.webhost);
});
}
app.post('/api/v1/shorten', postShort);
app.get('/api/v1/list', getList);
app.get('/:encoded_id', getEncodedID);
const server = app.listen(config.port, () => {
logger.info(`Server listening on port ${config.port}`);
});

View File

@ -2,23 +2,23 @@ const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const CounterSchema = Schema({
_id: { type: String, required: true },
seq: { type: Number, default: 1000 }
'_id': { 'type': String, 'required': true },
'seq': { 'type': Number, 'default': 1000 }
});
const counter = mongoose.model('counter', CounterSchema);
// create a schema for our links
const urlSchema = new Schema({
_id: { type: Number, index: true },
long_url: String,
created_at: Date,
visits: { type: Number, default: 0 }
'_id': { 'type': Number, 'index': true },
'long_url': String,
'created_at': Date,
'visits': { 'type': Number, 'default': 0 }
});
urlSchema.pre('save', function(next) {
const doc = this;
counter.findByIdAndUpdate({ _id: 'url_count' }, { $inc: { seq: 1 } }, function(error, counter) {
counter.findByIdAndUpdate({ '_id': 'url_count' }, { '$inc': { 'seq': 1 } }, function(error, counter) {
if (error)
return next(error);
doc.created_at = new Date();

View File

@ -1,16 +1,15 @@
function shorten() {
$.ajax({
url: '/api/v1/shorten',
type: 'POST',
dataType: 'JSON',
data: { url: $('#url-field').val() },
success: data => {
'url': '/api/v1/shorten',
'type': 'POST',
'dataType': 'JSON',
'data': { 'url': $('#url-field').val() },
'success': data => {
console.log('data', data);
const $link = $('#link');
const resultHTML = `<a class="result" href="${data.shortUrl}">${data.shortUrl}</a>`;
$link.html(resultHTML);
$link.hide().fadeIn('slow');
}
});
}
@ -18,7 +17,6 @@ function shorten() {
$('#url-field').keyup( (event) => {
if(event.keyCode === 13)
$('#btn-shorten').click();
});
$('#btn-shorten').on('click', () => {