-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
32 lines (26 loc) · 830 Bytes
/
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
// Load environment variables from .env file
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const Product = require('./models/product.model');
const productRoute = require('./routes/product.route');
const app = express();
// Middleware
app.use(express.json());
// Routes
app.use('/api/products', productRoute);
app.get('/', (req, res) => {
res.send("Hello from Node API server")
});
// Connect to MongoDB using environment variable
const dbUrl = process.env.DB_URL;
mongoose.connect(dbUrl)
.then(() => {
console.log("Connected to database!");
app.listen(4001, () => {
console.log("Server is running at port 4001");
});
})
.catch((error) => {
console.error("Could not connect to the database:", error);
});