-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (36 loc) · 1.48 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
//import main packages to work on
import express from 'express'; //webserver
import mongoose from 'mongoose'; //to connect to the mongo database
import dotenv from 'dotenv'; //to use environment variables
import bodyParser from 'body-parser'; //to parse the body of the request from the user's json
dotenv.config(); //to use the environment variables and the .env file
const app = express();
app.get('/', (req, res) => {
res.send('Hello World, MiniPost is here')
})
//setting up the server
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
//my routes
//define the source of the routes (import) and the path to the routes --> equivalent as a controller in springboot @GetMapping and return the response = app.use
//the files do have the functions to be done inside
import postRoute from './routes/post.js';
import getsRoute from './routes/gets.js';
import deleteRoute from './routes/delete.js';
import updateRoute from './routes/update.js';
app.use(bodyParser.json()); //to parse the body of the request from the user's json. the client needs to send the data in json format specifically (eg if sent by soap ui it wont work)
app.use('/posts', postRoute);
app.use('/posts', getsRoute);
app.use('/posts', deleteRoute);
app.use('/posts', updateRoute);
//mongo database
// Connect to MongoDB
mongoose.connect(process.env.DB_CONNECTOR, {
})
.then(() => {
console.log('Connected to MongoDB');
})
.catch((error) => {
console.error('Error connecting to MongoDB:', error);
});