-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (59 loc) · 1.82 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
import 'dotenv/config.js';
import express from 'express';
import cookieParser from 'cookie-parser';
const app = express();
import pool from './db.js';
import cors from 'cors';
import userRoutes from './routes/user.js';
import postRoutes from './routes/post.js';
import commentRoutes from './routes/comment.js';
import followRoutes from './routes/follow.js';
import likeRoutes from './routes/like.js';
import path from 'path';
async function getPostgresVersion() {
let client;
try {
client = await pool.connect();
const res = await client.query('SELECT version()');
console.log(res.rows[0]);
} catch (err) {
console.log('Err neon connection', err);
} finally {
client.release();
}
}
getPostgresVersion();
app.use(express.json());
app.use(cors({ credentials: true, origin: 'http://localhost:5173' }));
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use('/api', userRoutes);
app.use('/api', postRoutes);
app.use('/api', commentRoutes);
app.use('/api', followRoutes);
app.use('/api', likeRoutes);
if (process.env.NODE_ENV === 'production') {
const __dirname = path.resolve();
console.log(__dirname);
app.use(express.static(path.join(__dirname, '/client/dist')));
app.get('*', (req, res) =>
res.sendFile(path.resolve(__dirname, 'client', 'dist', 'index.html'))
);
} else {
// const __dirname = path.resolve();
// app.use('/uploads', express.static(path.join(__dirname, '/uploads')));
app.get('/', (req, res) => {
res.send('API is running....');
});
}
app.use((err, req, res, next) => {
const statusCode = res.statusCode ? res.statusCode : 500;
res.status(statusCode);
res.json({
message: err.message,
stack: process.env.NODE_ENV === 'development' ? err.stack : null,
});
});
app.listen(5000, () => {
console.log('server is running on port 5000');
});