-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
3,229 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
*.pid.lock | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# nyc test coverage | ||
.nyc_output | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# Bower dependency directory (https://bower.io/) | ||
bower_components | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (https://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directories | ||
node_modules/ | ||
jspm_packages/ | ||
|
||
# TypeScript v1 declaration files | ||
typings/ | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional eslint cache | ||
.eslintcache | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
*.tgz | ||
|
||
# Yarn Integrity file | ||
.yarn-integrity | ||
|
||
# dotenv environment variables file | ||
.env | ||
|
||
# next.js build output | ||
.next |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const monk = require('monk'); | ||
const connectionURL = process.env.MONGODB_URI || 'localhost/puny-li'; | ||
const db = monk(connectionURL); | ||
|
||
module.exports = db; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const Joi = require('joi'); | ||
const db = require('./connection'); | ||
|
||
const urls = db.get('urls'); | ||
|
||
const schema = Joi.object().keys({ | ||
name: Joi.string().token().min(1).max(100).required(), | ||
url: Joi.string().uri({ | ||
scheme: [ | ||
/https?/ | ||
] | ||
}) | ||
}).with('name', 'url'); | ||
|
||
function find(name) { | ||
return urls.findOne({ | ||
name | ||
}); | ||
} | ||
|
||
/* | ||
{ | ||
url: 'http://example.com', | ||
name: 'super-catchy' | ||
} | ||
*/ | ||
async function create(almostPuny) { | ||
const result = Joi.validate(almostPuny, schema); | ||
// result.error === null | ||
if (result.error === null) { | ||
const url = await urls.findOne({ | ||
name: almostPuny.name | ||
}); | ||
if (!url) { | ||
return urls.insert(almostPuny); | ||
} else { | ||
return Promise.reject({ | ||
isJoi: true, | ||
details: [{ | ||
message: 'Short name is in use.' | ||
}] | ||
}); | ||
} | ||
} else { | ||
return Promise.reject(result.error); | ||
} | ||
} | ||
|
||
module.exports = { | ||
create, | ||
find | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const express = require('express'); | ||
const morgan = require('morgan'); | ||
const bodyParser = require('body-parser'); | ||
|
||
const urls = require('./db/urls'); | ||
|
||
const app = express(); | ||
|
||
app.use(morgan('tiny')); | ||
app.use(bodyParser.json()); | ||
app.use(express.static('./public')); | ||
|
||
app.get('/:name', async (req, res) => { | ||
const puny = await urls.find(req.params.name); | ||
if (puny) { | ||
res.redirect(puny.url); | ||
} else { | ||
res.redirect(`/404.html?name=${req.params.name}`); | ||
} | ||
}); | ||
|
||
app.post('/api/puny', async (req, res) => { | ||
console.log(req.body); | ||
try { | ||
const url = await urls.create(req.body); | ||
res.json(url); | ||
} catch (error) { | ||
res.status(500); | ||
res.json(error); | ||
} | ||
}); | ||
|
||
const port = process.env.PORT || 5000; | ||
app.listen(port, () => { | ||
console.log(`listening on port ${port}`); | ||
}); |
Oops, something went wrong.