From 445a0c0411db3b6cc5891b3e6823131f45d34d65 Mon Sep 17 00:00:00 2001 From: Martin Donnelly Date: Mon, 25 Sep 2017 17:48:55 +0100 Subject: [PATCH] wip pre moving from bower to browserfy --- .eslintrc.json | 59 ++++++++++++++++++++--------------- app.js | 31 +++++++----------- base58.js | 22 ++++++------- models/url.js | 14 ++++----- public/javascripts/shorten.js | 12 +++---- 5 files changed, 68 insertions(+), 70 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 89a52a1..7042266 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -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": ["/"] }] } } diff --git a/app.js b/app.js index 28b803d..752a5d2 100644 --- a/app.js +++ b/app.js @@ -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}`); }); diff --git a/base58.js b/base58.js index fae6601..22d60cc 100644 --- a/base58.js +++ b/base58.js @@ -3,22 +3,22 @@ const alphabet = 'bMJZSrnxEyq8kN3UYQL5oXwV7BCFRtvpmDf1shAuHzKicTjeG29Pg4adW6'; const base = alphabet.length; -function encode(num){ - let encoded = ''; - while (num){ - const remainder = num % base; - num = Math.floor(num / base); +function encode(num) { + let encoded = ''; + while (num) { + const remainder = num % base; + num = Math.floor(num / base); encoded = alphabet[remainder].toString() + encoded; } return encoded; } -function decode(str){ - let decoded = 0; - while (str){ - const index = alphabet.indexOf(str[0]); - const power = str.length - 1; - decoded += index * (Math.pow(base, power)); +function decode(str) { + let decoded = 0; + while (str) { + const index = alphabet.indexOf(str[0]); + const power = str.length - 1; + decoded += index * (Math.pow(base, power)); str = str.substring(1); } return decoded; diff --git a/models/url.js b/models/url.js index 347679a..83e490c 100644 --- a/models/url.js +++ b/models/url.js @@ -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(); diff --git a/public/javascripts/shorten.js b/public/javascripts/shorten.js index b0beb67..4b47eb6 100644 --- a/public/javascripts/shorten.js +++ b/public/javascripts/shorten.js @@ -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 = `${data.shortUrl}`; $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', () => {