Skip to content
Browse files

as an api or as a service

  • Loading branch information...
1 parent 7043316 commit 5dbca2c1c377f2387dcc0627f7b51f499319a96a Edward Hotchkiss committed
Showing with 102 additions and 11 deletions.
  1. +44 −2 README.md
  2. 0 examples/{basic.js → api.js}
  3. +38 −0 examples/server.js
  4. +19 −8 models/ShortURL.js
  5. +1 −1 package.json
View
46 README.md
@@ -9,7 +9,7 @@ short
```bash
$ npm install short
```
-***Integrated***
+***Basic API Usage***
```javascript
var mongoose = require("mongoose");
@@ -31,10 +31,52 @@ short.make(URL, function(error, shortURL) {
var hash = shortURLObject[0].hash;
console.log(URL, hash);
process.exit(1);
- }
+ };
});
}
});
/* EOF */
+```
+
+***As a Server***
+
+```javascript
+var http = require("http");
+var mongoose = require("mongoose");
+var short = require("short");
+
+mongoose.connect("mongodb://localhost/short");
+
+var app = http.createServer(function(request, response) {
+ var hash = request.url.slice(1);
+ if (request.url === "/") {
+ response.writeHead(200, { "Content-Type" : "text/html" });
+ response.write("URL not found!");
+ response.end();
+ } else {
+ short.get(hash, function(error, shortURLObject) {
+ if (error) {
+ console.error(error);
+ } else {
+ if (shortURLObject) {
+ var URL = shortURLObject[0].URL;
+ response.writeHead(302, {
+ "Location" : URL
+ });
+ response.end();
+ } else {
+ response.writeHead(200, { "Content-Type" : "text/html" });
+ response.write("URL not found!");
+ response.end();
+ }
+ };
+ });
+ }
+});
+
+app.listen(8080);
+console.log("> Open http://localhost:8080/kQ4c");
+
+/* EOF */
```
View
0 examples/basic.js → examples/api.js
File renamed without changes.
View
38 examples/server.js
@@ -0,0 +1,38 @@
+
+var http = require("http");
+var mongoose = require("mongoose");
+var short = require("short");
+
+mongoose.connect("mongodb://localhost/short");
+
+var app = http.createServer(function(request, response) {
+ var hash = request.url.slice(1);
+ if (request.url === "/") {
+ response.writeHead(200, { "Content-Type" : "text/html" });
+ response.write("URL not found!");
+ response.end();
+ } else {
+ short.get(hash, function(error, shortURLObject) {
+ if (error) {
+ console.error(error);
+ } else {
+ if (shortURLObject) {
+ var URL = shortURLObject[0].URL;
+ response.writeHead(302, {
+ "Location" : URL
+ });
+ response.end();
+ } else {
+ response.writeHead(200, { "Content-Type" : "text/html" });
+ response.write("URL not found!");
+ response.end();
+ }
+ };
+ });
+ }
+});
+
+app.listen(8080);
+console.log("> Open http://localhost:8080/kQ4c");
+
+/* EOF */
View
27 models/ShortURL.js
@@ -38,14 +38,18 @@ ShortURL.findByHash = function(hash, callback) {
if (error) {
callback(error, null);
} else {
- var id = URL[0]._id;
- ShortURL.updateHitsById(id, function(error) {
- if (error) {
- callback(error, null);
- } else {
- callback(null, URL);
- };
- });
+ if (URL.length !== 0) {
+ var id = URL[0]._id;
+ ShortURL.updateHitsById(id, function(error) {
+ if (error) {
+ callback(error, null);
+ } else {
+ callback(null, URL);
+ };
+ });
+ } else {
+ callback(null, null);
+ }
};
});
};
@@ -62,6 +66,13 @@ ShortURL.updateHitsById = function(id, callback) {
});
};
+var nodeTiny = new ShortURL({
+ URL : "http://nodejs.org/",
+ hash : "kQ4c"
+});
+
+nodeTiny.save();
+
module.exports = {
shortURL_schema : shortURL_schema,
ShortURLSchema : ShortURLSchema,
View
2 package.json
@@ -2,7 +2,7 @@
"author": "Edward Hotchkiss <e@ingk.com>",
"name": "short",
"description": "Generate, retieve short urls over mongoose/mongodb/express",
- "version": "0.0.5",
+ "version": "0.0.6",
"repository": {
"type": "git",
"url": "git://github.com/edwardhotchkiss/short.git"

0 comments on commit 5dbca2c

Please sign in to comment.
Something went wrong with that request. Please try again.