Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var db = function () {
- this.mysql = require("mysql");
- var jsesc = require('jsesc');
- this.con = null;
- this.connectToDb = function(){
- this.con = this.mysql.createConnection({
- host: "localhost",
- user: "root",
- password: "root",
- database: "redirections",
- port: 8889
- });
- this.con.connect(function(err){
- if(err) throw err;
- console.log('Connection established');
- });
- };
- this.isNewUrl = function(url,callback){
- // sanitize
- var url = jsesc(url);
- this.con.query("SELECT COUNT('url_origine') AS urlCount FROM list WHERE url_origine = '"+url+"' ",function(err,res){
- if(err) throw err;
- var isNew = (res[0].urlCount > 0) ? false : true;
- callback(isNew);
- });
- }
- this.recordRedirection = function(dataToInsert){
- var that = this;
- // sanitize
- dataToInsert.url_destination = jsesc(dataToInsert.url_destination);
- that.con.query('INSERT INTO list SET ?', dataToInsert, function(err,res){
- if(err) throw err;
- console.log('Last insert ID:', res.insertId);
- });
- };
- };
- module.exports = db;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement