-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
69 lines (58 loc) · 1.66 KB
/
server.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
import express from 'express';
import mongoose from 'mongoose';
import cors from 'cors';
import dotenv from 'dotenv';
import User from './models/User.js';
dotenv.config();
const app = express();
const port = process.env.PORT;
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());
mongoose
.connect(process.env.MONGODB_CONNECTION_STRING, {
dbName: process.env.DATABASE,
})
.then(() => console.log('Connected to mongoDB'))
.catch(err => console.log('Databse connection failed!'));
app.post('/register', async (req, res) => {
// const user = new User(req.body);
try {
await User.create({
username: req.body.username,
password: req.body.password,
firstName: req.body.firstName,
lastName: req.body.lastName,
dateOfBirth: req.body.dateOfBirth,
email: req.body.email,
telephone: req.body.telephone,
gender: req.body.gender
});
res.status(200).json({
success: true,
message: 'User created'
});
} catch(error) {
console.log({error});
res.status(400).json({
message: 'Failed to create a user.'
});
}
});
app.get('/list', async (req, res) => {
try {
const users = await User.find({});
res.status(200).json({
success: true,
message: 'Here are all users',
data: users
});
} catch(error) {
res.status(400).json({
message: 'Failed to show all users'
});
}
});
app.listen(port, () => {
console.log('Server running on port', port)
});