-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
39 lines (35 loc) · 1.15 KB
/
Copy pathapp.js
File metadata and controls
39 lines (35 loc) · 1.15 KB
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
const express = require('express');
const mongoose = require('mongoose');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const cors = require('cors');
const taskRoutes = require('./routes/tasks');
const userRoutes = require('./routes/userRoutes')
// Create an Express application
const app = express();
//configuration on prot
const port = 3000;
// Enable cors at the server side.
const corsOption = {
origin: ['http://localhost:3000'],
credentials: true,
methods: ["GET", "POST", "PUT", "DELETE","PATCH"],
}
// Enable CORS to handle cross-origin errors
app.use(cors(corsOption));
// Middleware to log requests
app.use(morgan('combined'));
// Middleware to parse incoming requests with JSON payloads
app.use(bodyParser.json());
app.use(express.json());
// Connect to MongoDB
mongoose.connect('mongodb://node-mongo:27017/node-api')
.then(() => console.log('Connected to MongoDB'))
.catch((err) => console.log('Error connecting to MongoDB:', err));
// Routes
app.use('/users', userRoutes);
app.use('/tasks', taskRoutes);
// Start the server
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}/`);
});