-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
97 lines (84 loc) · 2.81 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
var express = require('express')
var mongoose = require('mongoose');
var bodyParser = require('body-parser')
var randtoken = require('rand-token');
var app = express();
mongoose.connect('mongodb://user:pass@ds237735.mlab.com:37735/codeoff');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
// we're connected!
console.log("Connected To MongoLab");
});
//Schema Setup
var codeSchema = mongoose.Schema({
code: String
});
var Code = mongoose.model('codecollections', codeSchema);
app.use(require('body-parser').urlencoded({
extended: true
}));
app.set('port', process.env.PORT || 3000);
app.route('/api/v1/:tokenKey/:type/generate')
.get(function(req, res) {
var typeOfCode = req.params.type;
var genCode;
if(typeOfCode==1)
genCode = generateCode1();
else if(typeOfCode==2)
genCode = generateCode2();
else if(typeOfCode==3)
genCode = generateCode3();
else
genCode = generateCode();
var newCode = new Code({ code: genCode });
newCode.save(function (err, result) {
if (err) return console.error(err);
console.log("Saved");
});
res.json(newCode);
});
app.route('/api/v1/:tokenKey/validate')
.post(function(req, res) {
Code.findOne({
code: req.body.codeStr
}, function(err, successStatus) {
if (err)
res.send({
status: "error"
});
else{
if(successStatus!=null)
res.send({
status: "success"
});
else
res.send({
status: "failure"
});
}
});
});
var generateCode = function() {
var generatedCode = randtoken.generate(4) + "-" + randtoken.generate(4) + "-" + randtoken.generate(4);
generatedCode = generatedCode.toUpperCase();
return generatedCode;
}
var generateCode1 = function() {
var generatedCode = randtoken.generate(2) + "-" + randtoken.generate(2) + "-" + randtoken.generate(2) + "-" + randtoken.generate(2) + "-" + randtoken.generate(2);
generatedCode = generatedCode.toUpperCase();
return generatedCode;
}
var generateCode2 = function() {
var generatedCode = randtoken.generate(10);
generatedCode = generatedCode.toUpperCase();
return generatedCode;
}
var generateCode3 = function() {
var generatedCode = randtoken.generate(4) + "-" + randtoken.generate(2) + "-" + randtoken.generate(4);
generatedCode = generatedCode.toUpperCase();
return generatedCode;
}
app.listen(app.get('port'), function() {
console.log('server started')
});