This repository has been archived by the owner on Sep 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
57 lines (47 loc) · 1.62 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
const express = require('express');
const app = express();
const helmet = require('helmet');
require('dotenv').config();
app.use(express.json());
app.use(helmet());
// Item handlers
const passwordsGet = require('./modules/passwords/fetch.js');
const passwordsCreate = require('./modules/passwords/create.js');
const notesGet = require('./modules/notes/fetch.js');
const notesCreate = require('./modules/notes/create.js');
// Database handler
const connectDB = require('./modules/database/connect.js');
connectDB();
// Authentication handlers
const register = require('./modules/auth/register.js');
const login = require('./modules/auth/login.js');
app.all('/', (req, res) => {
return res.json({ status: 0, message: 'Why don\'t you try calling the API endpoints?' });
});
app.get('/api/fetch/:item', (req, res) => {
if (req.params.item === 'passwords') {
passwordsGet(req, res);
} else if (req.params.item === 'notes') {
notesGet(req, res);
} else {
return res.json({ status: 0, message: "Please call a valid API endpoint!" });
}
});
app.post('/api/create/:item', (req, res) => {
if (req.params.item === 'passwords') {
passwordsCreate(req, res);
} else if (req.params.item === 'notes') {
notesCreate(req, res);
} else {
return res.json({ status: 0, message: "Please call a valid API endpoint!" });
}
});
app.post('/api/auth/register', (req, res) => {
register(req, res);
});
app.post('/api/auth/login', (req, res) => {
login(req, res);
});
app.listen(process.env.PORT, () => {
console.log('Server is running on http://localhost:' + process.env.PORT);
});