-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
59 lines (49 loc) · 1.45 KB
/
app.js
File metadata and controls
59 lines (49 loc) · 1.45 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
const express = require('express');
const dotenv = require('dotenv');
const cors = require('cors')
const cookieParser = require("cookie-parser");
const app = express();
dotenv.config({ path: "./config.env" });
require('./db/conn.js');
app.use(express.json());
app.use(cors());
app.use(cookieParser());
app.use(express.urlencoded({ extended: false }));
//? model
const User = require('./model/userSchema.js');
//? routes
app.use(require('./router/auth.js'));
const PORT = process.env.PORT || 6450;
app.get('/api/usersprofiles', async (req, res) => {
try {
const users = await User.find();
res.status(200).json(users);
} catch (error) {
console.log(error);
}
});
app.get('/api/usersprofiles/:id', async (req, res) => {
try {
const _id = req.params.id;
const user = await User.findById(_id);
if (user) {
res.status(200).json(user);
}
else {
return res.status(404).json({ error: 'No Data Found! Try Again' });
}
} catch (error) {
console.log(error);
return res.status(500).json({ error: 'Internal Server Error' });
}
});
if (process.env.NODE_ENV == 'production') {
app.use(express.static("client/build"));
const path = require("path");
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
app.listen(PORT, () => {
console.log(`Server Running At Port ${PORT}`);
});