This commit is contained in:
Martin Donnelly 2017-09-05 15:53:07 +01:00
parent eb0b076fed
commit d4bab117da
6 changed files with 159 additions and 115 deletions

View File

@ -1,24 +1,46 @@
{
"plugins": [
"react"
],
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
"jsx": false
}
},
"env": {
"es6": true,
"browser": true,
"node": true,
"mocha": true
"es6": true
},
"extends": [
"eslint:recommended",
"plugin:react/recommended"
],
"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,
"curly": [1, "multi"],
"no-mixed-spaces-and-tabs": 1,
"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"]
}
}

50
app.js
View File

@ -25,7 +25,8 @@ process.env.NODE_ENV = process.env.NODE_ENV || 'dev';
if (process.env.NODE_ENV === 'prod') {
isProduction = true;
sitePath = 'live';
indexView = 'dist/index.html'
indexView = 'dist/index.html';
config.webhost = 'http://nurl.co/';
}
logger.warn(`isProduction:${isProduction}`);
@ -39,17 +40,17 @@ app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, sitePath)));
app.use(favicon(__dirname + '/live/favicon-16x16.png'));
app.all('/*', function(req, res, next) {
app.all('/*', (req, res, next) => {
// CORS headers
res.header("Access-Control-Allow-Origin", "*"); // restrict it to the required domain
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') {
if (req.method === 'OPTIONS')
res.status(200).end();
} else {
else
next();
}
});
app.get('/', function(req, res) {
@ -60,12 +61,12 @@ app.get('/admin/list', function(req, res){
res.sendFile(path.join(__dirname, listView));
});
app.post('/api/v1/shorten', function(req, res){
function postShort(req, res) {
const longUrl = req.body.url;
let shortUrl = '';
// check if url already exists in database
Url.findOne({long_url: longUrl}, function (err, doc){
Url.findOne({ long_url: longUrl }, (err, doc) => {
if (doc) {
shortUrl = config.webhost + base58.encode(doc._id);
@ -80,9 +81,9 @@ app.post('/api/v1/shorten', function(req, res){
// save the new link
newUrl.save(function (err) {
if (err){
if (err)
logger.error(err);
}
shortUrl = config.webhost + base58.encode(newUrl._id);
@ -92,35 +93,38 @@ app.post('/api/v1/shorten', function(req, res){
});
});
app.get('/api/v1/list', function(req, res){
Url.find({}, function(err, doc) {
if (doc) {
logger.debug(doc);
}
})
function getList(req, res) {
Url.find({}, (err, doc) => {
if (doc)
logger.debug(doc);
});
}
app.get('/:encoded_id', function(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}}, function (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, function () {
const server = app.listen(config.port, () => {
logger.info(`Server listening on port ${config.port}`);
});

View File

@ -85,3 +85,21 @@ body {
width: 700px;
}
}
body, html{
font-family: "Roboto", "Arial", sans-serif;
font-weight: normal;
font-style: normal;
}
h1{
font-family: "Roboto Slab", monospace;
font-weight: 700;
font-style: normal;
}
h2, h3, h4{
font-family: "Ubuntu", sans-serif;
font-weight: 500;
font-style: normal;
}

View File

@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>nURL</title>
<link href="https://fonts.googleapis.com/css?family=Roboto+Condensed" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Roboto+Slab:700-Ubuntu:500-Roboto:regular" rel="stylesheet">
<!-- build:css-->
<link rel="stylesheet" href="//cdn.muicss.com/mui-0.9.20/css/mui.min.css">
<link href="css/styles.css" rel="stylesheet">