-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·53 lines (42 loc) · 1.38 KB
/
Copy pathapp.js
File metadata and controls
executable file
·53 lines (42 loc) · 1.38 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
// V0.1
import express from 'express';
import bcrypt from 'bcrypt';
import fs from 'node:fs';
import path from 'node:path';
import cookieParser from 'cookie-parser';
import dotenv from 'dotenv';
// routes, controller import
import { registerRoute } from './routes/register.js';
import { loginRoute } from './routes/login.js';
import { profileRoute } from './routes/profile.js';
// middleware import
import { authenticateToken } from './middleware/auth.js';
dotenv.config();
const app = express();
const PORT = 3000;
const dbPath = `./data/db.json`;
// checking if dbPath exists
if (fs.existsSync(dbPath)) {
console.log('Continuing with an existing db file...');
}
// else creating file
else {
const dbArray = [];
fs.writeFileSync(dbPath, JSON.stringify(dbArray, null, 2));
}
// middlwares
// cookieParser which attaches parsed cookies data to req aka req.cookies
app.use(cookieParser());
// get requests in json format
app.use(express.json());
// get req.body working and curl url -d '' - flag fix
app.use(express.urlencoded({ extended: true }));
// register post request V1.0.0
app.post('/register', registerRoute);
// login post request V1.0.0
app.post('/login', loginRoute);
// profile post request V1.0.0
app.post('/profile', authenticateToken, profileRoute);
app.listen(PORT, () => {
console.log('Okay server is running...');
})