Skip to content
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
43 changes: 19 additions & 24 deletions backend/Node.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
Sure! Here's the code for the `./backend/Node.js` file:

```javascript
// Import required modules
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");

// Create an instance of Express
const app = express();
const expressApp = express();

// Set up body-parser middleware
app.use(bodyParser.urlencoded({ extended: false }));
Expand All @@ -16,42 +13,40 @@ app.use(bodyParser.json());
// Connect to MongoDB database
/**
* Connect to the MongoDB database.
*
*
* @returns {Promise<void>} A promise that resolves when the connection is successful.
*/
mongoose.connect('mongodb://localhost/mydatabase', { useNewUrlParser: true, useUnifiedTopology: true })
mongoose
.connect("mongodb://localhost/mydatabase", {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log('Connected to MongoDB');
console.log("Connected to MongoDB");
})
.catch((error) => {
console.error('Error connecting to MongoDB:', error);
console.error("Error connecting to MongoDB:", error);
});

// Define routes
/**
* Handle GET request for the root URL ("/").
*
*
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @returns {void}
*/
app.get('/', (req, res) => {
res.send('Hello, world!');
expressApp.get("/", (req, res) => {
res.send("Hello, world!");
});

// Start the server
const port = process.env.PORT || 3000;
const serverPort = process.env.PORT || 3000;
/**
* Start the server and listen on the specified port.
*
*
* @returns {void}
*/
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
expressApp.listen(serverPort, () => {
console.log(`Server is running on port ${serverPort}`);
});

This code sets up a basic Node.js server using Express framework. It includes the necessary imports, middleware setup, MongoDB connection, and a simple route for the root URL ("/"). The server listens on the specified port (default is 3000) and logs a message when it starts running.

Please note that you may need to modify the MongoDB connection URL (`mongodb://localhost/mydatabase`) based on your specific MongoDB setup.

Let me know if you need any further assistance!
8 changes: 4 additions & 4 deletions database/MongoDB
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
* @param {Object} res - The response object.
* @returns {void}
*/
app.get('/', (req, res) => {
expressApp.get('/', (req, res) => {
res.send('Hello, world!');
});

// Start the server
const port = process.env.PORT || 3000;
const serverPort = process.env.PORT || 3000;
/**
* Start the server and listen on the specified port.
*
* @returns {void}
*/
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
expressApp.listen(serverPort, () => {
console.log(`Server is running on port ${serverPort}`);
});