This repository has been archived by the owner on Apr 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.js
166 lines (133 loc) · 4.29 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var express = require('express');
var bcrypt= require('bcrypt');
var path = require('path')
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var msg91 = require("msg91")("197611ACBvIjqJZM5a7ed724","AAYYUU", "4" );
var app = express();
var multer = require('multer');
var bodyParser = require('body-parser');
const uri = "mongodb://aniket:[email protected]:57349/users-alphynite"
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET','POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
app.use('/uploads/', express.static(path.join(__dirname, '/public/uploads')));
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, path.join(__dirname + '/public/uploads'))
},
filename: function (req, file, cb) {
cb( null,file.originalname+Date.now()+".jpg",
)
}
})
let upload = multer({storage: storage, fileFilter: function (req, file, callback) {
var ext = path.extname(file.originalname);
if(ext !== '.png' && ext !== '.jpg' && ext !== '.gif' && ext !== '.jpeg') {
return callback(new Error('Only images are allowed'))
}
callback(null, true)
},
limits:{
fileSize: 1024 * 1024
}});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.post('/api/upload',upload.single('photo'), function (req, res,err) {
if (!req.file) {
console.log("No file received");
return res.send({
success: false
});
} else {
console.log("finally");
console.log(req.file.filename)
console.log('file received');
console.log()
return res.send(JSON.stringify([{
"success": true,
"fileurl":req.file.filename
}]))
}
});
app.post("/api/sendotp/",function(req,res){
mongoose.connect(uri,{useNewUrlParser:true});
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
console.log(req.body[0]['OTP'])
var mobileNo = req.body[0]['data']['Phone'];
var data = req.body[0]['data'];
const saltRounds = 10;
bcrypt.hash(data.Password, saltRounds, function(err, hash) {
if(err){
console.log("Error");
}
else{
data.Password = hash;
db.once('open', function() {
console.log("Connected");
db.collection('Users').insertOne(data);
});
}
})
var Message="Hi ! You OTP is "+req.body[0]['OTP'];
msg91.send(mobileNo, Message, function(err, response){
console.log(err);
console.log(response);
});
console.log(req.connection.remoteAddress);
res.send(["Done"]);
});
app.post("/api/login",function(req,res){
mongoose.connect(uri,{useNewUrlParser:true});
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
var phone = req.body.Phone + '';
var password = req.body.Password;
db.collection('Users').findOne({'Phone':phone}, (err,doc) => {
if(err){
console.log("Error");
}
else{
let auth = false;
if( doc != null){
bcrypt.compare(password, doc.Password, (err,val) => {
if(!err){
if(val){
auth = true;
}
}
res.send(auth);
})
}
else{
res.send(auth);
}
}
});
});
app.post("/api/signup/",function(req,res){
console.log(req.body[0]['data']['Phone']);
const saltRounds = 10;
bcrypt.hash(req.body[0]['data']['Phone'], saltRounds, function(err, hash) {
console.log(hash);
res.status(200).send({"hash":String(hash)});
});
})
app.post("/api/product",function(req,res){
console.log("got")
console.log(req.body);
res.send(["Done"])
})
app.use(express.static(__dirname + '/dist/frontend/'))
app.get('/*',function(req,res){
res.sendFile(path.join(__dirname+'/dist/frontend/index.html'))
})
var port = process.env.PORT||3000;
app.listen(port,console.log(`Your server available at http://localhost:${port}`));