Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**********************
- * ./init.js
- **********************/
- require('./Lib/MooTools').apply(GLOBAL);
- var http = require('http');
- http.ServerResponse.implement({
- header : function (code, plain) {
- this.writeHead(code, {
- 'Content-Type': plain ? 'text/plain' : 'text/html'
- });
- },
- redirect : function (url, status) {
- this.writeHead(status || 302, {
- 'Content-Type' : 'text/plain',
- 'Location' : url
- });
- this.write('Redirecting to ' + url);
- this.end();
- }
- });
- var conn = require('./Lib/mysql/mysql-libmysqlclient').createConnectionSync();
- conn.connectSync('localhost', 'NAME', 'PASS', 'nodejs');
- var linkModel = new (require('./Link').Model)(conn);
- var renderer = new (require('./Renderer').Renderer)(linkModel);
- http.createServer(function (req, res) {
- renderer.run(req, res);
- }).listen(8124, "127.0.0.1");
- console.log('Server running at http://127.0.0.1:8124/');
- /**********************
- * ./Link.js
- **********************/
- require('./Lib/MooTools').apply(GLOBAL);
- Link = new Class({
- initialize : function (obj) {
- this.setId(obj.id).id || this.setCode(obj.code);
- this.setUrl(obj.url);
- },
- setId : function (id) {
- this.id = (isNaN(id) || id <= 0) ? null : parseInt(id);
- return this;
- },
- getId : function () {
- return this.id;
- },
- setUrl : function (url) {
- this.url = url || null;
- return this;
- },
- getUrl : function () {
- return this.url;
- },
- setCode : function (code) {
- this.id = Link.fromCode(code);
- return this;
- },
- getCode : function () {
- return Link.getCode(this.id);
- },
- });
- Link.getCode = function (id) {
- return id ? id.toString(36) : null;
- };
- Link.fromCode = function (code) {
- return code ? parseInt(code, 36) : null;
- };
- Model = new Class({
- conn : null,
- initialize : function (conn) {
- this.conn = conn;
- },
- create : function (args) {
- return new Link(args);
- },
- get : function (link, fn) {
- if (!link.getId()) throw 'EmptyId';
- var q = 'SELECT * FROM `shortLinks` WHERE `id` = ' + link.getId();
- this.conn.query(q, function (err, res) {
- if (err) throw err;
- res.fetchAll(function (err, rows) {
- if (err) throw err;
- fn(rows.length ? link.setUrl(rows[0].url) : null);
- res.freeSync();
- });
- });
- },
- put : function (link, fn) {
- var q = 'INSERT INTO `shortLinks` (`id`, `url`) ' +
- 'VALUES (NULL , "' + this.conn.escapeSync(link.getUrl()) + '");'
- this.conn.query(q, function (err, res) {
- if (err) throw err;
- fn(link.setId(
- this.conn.lastInsertIdSync()
- ));
- }.bind(this));
- }
- });
- exports.Link = Link;
- exports.Model = Model;
- /**********************
- * ./Renderer.js
- **********************/
- require('./Lib/MooTools').apply(GLOBAL);
- var url = require('url');
- var fs = require('fs');
- exports.Renderer = new Class({
- initialize : function (linkModel) {
- this.link = linkModel;
- },
- run : function (req, res) {
- var path = url.parse(req.url, true);
- if (path.query && 'add' in path.query) {
- var addUrl = path.query.add;
- if (!url.parse(addUrl).protocol) {
- addUrl = 'http://' + addUrl;
- }
- this.add(res, addUrl);
- } else if (path.pathname.test(/^\/![0-9a-z]+$/)) {
- this.send(res, path.pathname.substr(2));
- } else {
- this.index(res);
- }
- },
- add : function (res, url) {
- res.header(200);
- this.link.put(
- this.link.create({ url : url }),
- function (link) {
- res.end(link.getCode());
- }
- );
- },
- send : function (res, code) {
- this.link.get(
- this.link.create({ code : code }),
- function (link) {
- if (link) {
- res.redirect(link.getUrl());
- } else {
- res.header(404, true);
- res.end('There is not such url');
- }
- }
- );
- },
- index : function (res) {
- fs.readFile(__dirname + '/index.html', function (err, data) {
- if (err) throw err;
- res.header(200);
- res.end(data);
- })
- }
- });
- /**********************
- * ./index.html
- **********************/
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Сокращатель ссылок на node.js</title>
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
- <script>
- $(function () {
- $('input[type=submit]').click(function() {
- var input = $('input[type=text]');
- var url = input.val();
- input.val('');
- url && $.ajax({
- url : './',
- data: ({ add : url }),
- success : function (data) {
- var result = location.protocol + '//' + location.host + '/!' + data;
- $('#url')
- .prepend(
- $('<dd>').append(
- $('<a>')
- .text(result)
- .attr('href', result)
- )
- .hide()
- .fadeIn()
- )
- .prepend(
- $('<dt>')
- .text(url)
- .hide()
- .fadeIn()
- );
- } // success function
- }); // ajax object
- }); // $('input[type=submit]').click
- }); // document.ready
- </script>
- </head>
- <body>
- <div id="form">
- <input type="text" />
- <input type="submit" />
- </div>
- <dl id="url"></dl>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement