Skip to content

structuring of backend side, env file, modularization added #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
PORT=3000
DB_USER=postgres
DB_HOST=localhost
DB_NAME=events_db
DB_PASSWORD=password
DB_PORT=5432
CORS_ORIGIN=http://localhost:5173
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "technexus-be",
"version": "1.0.0",
"description": "",
"main": "index.js",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand All @@ -12,6 +12,7 @@
"dependencies": {
"body-parser": "^1.20.3",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.21.1",
"pg": "^8.13.0"
}
Expand Down
13 changes: 13 additions & 0 deletions src/config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require('dotenv').config();

module.exports = {
port: process.env.PORT || 3000,
db: {
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_NAME,
password: process.env.DB_PASSWORD,
port: process.env.DB_PORT,
},
corsOrigin: process.env.CORS_ORIGIN || 'http://localhost:5173',
};
65 changes: 18 additions & 47 deletions index.js → src/controllers/eventController.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,38 @@
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const { Pool } = require('pg');
const pool = require('../models/db');

const app = express();
const port = 3000;

const pool = new Pool({
user: 'postgres',
host: 'localhost',
database: 'events_db',
password: 'password',
port: 5432,
});

// Use CORS to allow requests from the React app
app.use(cors({
origin: 'http://localhost:5173'
}));
app.use(bodyParser.json());

// Define a simple route
app.get('/', (req, res) => {
res.send('Hello from the Node.js backend!');
});

app.get('/events', async (req, res) => {
const getAllEvents = async (req, res) => {
try {
const result = await pool.query('SELECT * FROM events');
res.json(result.rows);
} catch (err) {
console.error(err);
res.status(500).send('Server error');
}
});
};

app.get('/events/online', async (req, res) => {
const getOnlineEvents = async (req, res) => {
try {
console.log('in online events');
const result = await pool.query(`SELECT * FROM events WHERE type = 'online'`);
console.log('online events:', result.rows);
res.json(result.rows);
} catch (err) {
console.error(err);
res.status(500).send('Server error');
}
});
};

app.get('/events/offline', async (req, res) => {
const getOfflineEvents = async (req, res) => {
try {
const result = await pool.query(`SELECT * FROM events WHERE type = 'offline'`);
res.json(result.rows);
} catch (err) {
console.error(err);
res.status(500).send('Server error');
}
});

//write a post request for api : return axios.post(`${API_BASE_URL}/createevent`, eventData);


app.post('/createevent', (req, res) => {
};

const createEvent = async (req, res) => {
console.log('reaching create event api.');
const title = req.body.title ?? '';
const date = req.body.date ?? '';
const description = req.body.description ?? '';
Expand All @@ -77,7 +49,6 @@ app.post('/createevent', (req, res) => {
const state = location.state ?? '';
const postalCode = location.postalCode ?? '';


console.log(image, link, lat, lng, amenity, city, country, state, postalCode, title, description, date);
const query = `
INSERT INTO events (title, date,type, description, image, link, lat, lng, amenity, city, country, state, postalCode)
Expand All @@ -97,11 +68,11 @@ app.post('/createevent', (req, res) => {
console.error('Error inserting event data:', err);
res.status(500).json({ error: 'Failed to create event' });
}
});



// Start the server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
};

module.exports = {
getAllEvents,
getOnlineEvents,
getOfflineEvents,
createEvent,
};
18 changes: 18 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const config = require('./config/config');
const eventRoutes = require('./routes/eventRoutes');
const errorHandler = require('./middlewares/errorHandler');

const app = express();

app.use(cors({ origin: config.corsOrigin }));
app.use(bodyParser.json());

app.use('/events', eventRoutes);
app.use(errorHandler);

app.listen(config.port, () => {
console.log(`Server is running on http://localhost:${config.port}`);
});
6 changes: 6 additions & 0 deletions src/middlewares/errorHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const errorHandler = (err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Something broke!');
};

module.exports = errorHandler;
6 changes: 6 additions & 0 deletions src/models/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { Pool } = require('pg');
const config = require('../config/config');

const pool = new Pool(config.db);

module.exports = pool;
11 changes: 11 additions & 0 deletions src/routes/eventRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const express = require('express');
const router = express.Router();
const eventController = require('../controllers/eventController');


router.get('/', eventController.getAllEvents);
router.get('/online', eventController.getOnlineEvents);
router.get('/offline', eventController.getOfflineEvents);
router.post('/createevent', eventController.createEvent);

module.exports = router;