2023-06-08 14:53:50 +00:00
|
|
|
import express from 'express';
|
2023-06-08 16:03:27 +00:00
|
|
|
import mongoose from 'mongoose';
|
2023-06-09 15:39:20 +00:00
|
|
|
import cookieParser from 'cookie-parser';
|
2023-06-08 17:08:15 +00:00
|
|
|
|
|
|
|
import userRouter from './routes/user';
|
2023-06-09 15:39:20 +00:00
|
|
|
import productRouter from './routes/product';
|
2023-06-09 21:03:43 +00:00
|
|
|
import cartRouter from './routes/cart';
|
2023-06-08 15:50:54 +00:00
|
|
|
|
2023-06-08 16:03:27 +00:00
|
|
|
const env = require('dotenv').config().parsed;
|
|
|
|
|
2023-06-08 14:53:50 +00:00
|
|
|
const app = express();
|
2023-06-08 15:50:54 +00:00
|
|
|
const PORT = 3000;
|
2023-06-08 14:53:50 +00:00
|
|
|
|
|
|
|
app.use(express.json());
|
2023-06-09 15:39:20 +00:00
|
|
|
app.use(cookieParser())
|
2023-06-08 14:53:50 +00:00
|
|
|
|
2023-06-08 17:08:15 +00:00
|
|
|
|
2023-06-08 16:03:27 +00:00
|
|
|
// Connect to MongoDB using Mongoose
|
|
|
|
mongoose.connect(env.DATABASE_URL);
|
2023-06-08 14:53:50 +00:00
|
|
|
|
2023-06-08 16:03:27 +00:00
|
|
|
const db = mongoose.connection;
|
2023-06-08 14:53:50 +00:00
|
|
|
|
2023-06-08 16:53:03 +00:00
|
|
|
// Check for DB connection
|
2023-06-09 21:03:43 +00:00
|
|
|
db.on('error', () => {
|
|
|
|
console.error.bind(console, 'MongoDB connection error:')
|
|
|
|
process.exit(1);
|
|
|
|
});
|
2023-06-08 16:03:27 +00:00
|
|
|
db.once('open', () => {
|
|
|
|
console.log('Connected to MongoDB');
|
|
|
|
});
|
2023-06-08 14:53:50 +00:00
|
|
|
|
2023-06-08 16:53:03 +00:00
|
|
|
// Routes
|
2023-06-08 17:08:15 +00:00
|
|
|
app.use('/users', userRouter);
|
2023-06-09 15:39:20 +00:00
|
|
|
app.use('/products', productRouter);
|
2023-06-09 21:03:43 +00:00
|
|
|
app.use('/cart', cartRouter);
|
2023-06-08 14:53:50 +00:00
|
|
|
|
2023-06-08 16:53:03 +00:00
|
|
|
// Start server
|
2023-06-08 16:03:27 +00:00
|
|
|
app.listen(PORT, () => {
|
|
|
|
console.log(`Server started on port ${PORT}`);
|
|
|
|
});
|