31 lines
729 B
TypeScript
31 lines
729 B
TypeScript
import express from 'express';
|
|
import mongoose from 'mongoose';
|
|
import { authenticateToken } from './middlewares/authMiddleware';
|
|
import userRoutes from './routes/user';
|
|
|
|
const env = require('dotenv').config().parsed;
|
|
|
|
const app = express();
|
|
const PORT = 3000;
|
|
|
|
app.use(express.json());
|
|
app.use(authenticateToken);
|
|
|
|
// Connect to MongoDB using Mongoose
|
|
mongoose.connect(env.DATABASE_URL);
|
|
|
|
const db = mongoose.connection;
|
|
|
|
// Check for DB connection
|
|
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
|
|
db.once('open', () => {
|
|
console.log('Connected to MongoDB');
|
|
});
|
|
|
|
// Routes
|
|
app.use('/users', userRoutes);
|
|
|
|
// Start server
|
|
app.listen(PORT, () => {
|
|
console.log(`Server started on port ${PORT}`);
|
|
});
|