-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
93 lines (72 loc) · 2.29 KB
/
index.js
File metadata and controls
93 lines (72 loc) · 2.29 KB
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
import express from 'express'
import cors from 'cors'
const app = express()
import mongoose from 'mongoose'
async function dbstart(){
const db = await mongoose.connect('mongodb://localhost/TestProject',(e) => {console.log("DATABASE CONNECTED")});
}
dbstart()
const Schema = mongoose.Schema
const ObjectId = Schema.ObjectId
const RegisterPost = new Schema({
user: String,
password: String
})
const RegisterModel = mongoose.model('RegisterPost',RegisterPost)
const post = new RegisterModel();
const cors_opt = {
origin: '*',
optionsSuccessStatus: 200
}
app.use(cors())
app.get('/',cors(cors_opt),(req,res) => {
res.json({msg: 'CORS ENABLED'})
})
// eaoeoaekapoea
app.post('/register',cors(cors_opt),(req,res) => {
const user = req.headers.user
const password = req.headers.password
RegisterModel.find({'user': user},'user',(err,docs) => {
if (err) {
console.log('Error /login '+err)
res.send('no')
}else{
console.log('Connected find '+docs+' '+typeof(docs)+' '+String(docs).length)
if (String(docs).length <= 0){
const small = new RegisterModel({ user: user,password: password });
small.save((err) => {
if (err){
console.log('Error /register '+err)
};
});
res.statusCode = 200;
res.send('yes')
}else{
res.statusCode = 403;
res.send('no');
}
}
})
})
app.post('/login',cors(cors_opt),(req,res) => {
const user = req.headers.user
const password = req.headers.password
//const search = new RegisterModel({ user: user,password: password });
const User = mongoose.model('RegisterPost',RegisterPost)
RegisterModel.find({'user': user,'password': password},'user',(err,docs) => {
if (err) {
console.log('Error /login '+err)
res.send('no')
}else{
console.log('Connected find '+docs+' '+typeof(docs)+' '+String(docs).length)
if (String(docs).length > 0){
res.send('yes')
}else{
res.send('no')
}
}
})
})
app.listen(3000,() => {
console.log('API DISPONIBLE PORT 3000')
})