-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
36 lines (29 loc) · 1.1 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
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const path = require('path');
const app = express();
const connection = require('./db');
// Middleware
app.use(cors()); // Enable CORS for all routes
app.use(express.json()); // Parse JSON payloads
// Connect to the database
connection();
// Serve static files from the uploads directory
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
// Routes
app.use('/api/auth', require('./routes/auth')); // Authentication routes (login, register, etc.)
app.use('/api/users', require('./routes/users')); // User-related routes
app.use('/api/posts', require('./routes/posts')); // Post-related routes
// Test route to check server status
app.get('/', (req, res) => {
res.send('Server is running!');
});
// Error handling middleware (optional, but recommended)
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Something went wrong!');
});
// Start the server
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`Listening on port ${port}...`));