-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
106 lines (83 loc) · 2.49 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
require('dotenv').config();
const bodyParser = require('body-parser');
const express = require('express');
const { default: mongoose } = require('mongoose');
const cors = require('cors')
const app = express()
const PORT = process.env.PORT || 3000;
const conn = process.env.CONN;
const allowCrossDomain = (req, res, next) => {
res.header(`Access-Control-Allow-Origin`, `*`);
res.header(`Access-Control-Allow-Methods`, `GET,PUT,POST,DELETE`);
res.header(`Access-Control-Allow-Headers`, `Content-Type`);
next();
};
app.use(cors())
app.use(allowCrossDomain)
app.options('*', cors())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
mongoose.connect(conn)
.then(() => console.log('connected'))
.catch((err) => console.log(err))
// create a user --> temp
const demoSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
age: {
type: Number
},
salary: {
type: Number
},
email: {
type: String,
required: true
}
})
const user = mongoose.model('mymodel', demoSchema, 'demodata')
// GET Request (ENDPOINTS)
app.get("/", (req, res) => {
res.end("Hello world")
})
app.get("/about", (req, res) => {
res.end("welcome to about page")
})
// http://localhost:8080/name/aaryan
app.get("/name/:myname", (req, res) => {
res.end("welcome " + req.params.myname)
})
// POST Request (ENDPOINTS)
// TODO:body-parser
app.post("/login", (req, res) => {
const body = req.body;
const username = body.username;
const pass = body.pass;
if(username === "aryan" && pass === "123")
res.status(200).json({
data: "success",
})
else
res.status(404).end("Incorrect creds")
})
app.post('/create', async (req, res) => {
const body = req.body;
const name = body.name;
const age = body.age;
const salary = body.salary;
const email = body.email;
const insertedUser = await user.create({name: name, age: age, salary: salary, email: email})
res.json({msg: "User inserted successfully", data: insertedUser})
})
app.get('/count', async (req, res) =>{
res.json({count: await user.countDocuments()})
})
app.get('/id/:name', async (req, res) => {
const name = req.params.name;
const namedUser = await user.find({name: name})//.where('name').equals(name);
res.json({msg: "success", data: namedUser})
})
// http://localhost:8080/
app.listen(PORT, () => console.log("Application started on PORT " + PORT))