Skip to content

Commit 3502630

Browse files
authored
Modify according to render FE components through nodejs app (#5)
1 parent 4872000 commit 3502630

4 files changed

Lines changed: 24 additions & 127 deletions

File tree

.env.example

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
MONGO_URI=your_mongodb_url
2+
PORT=your_port #8080
3+
HOST=your_host #localhost
4+
NODE_ENV=your_env # production
5+
JWT_SECRET=your_secret_key_here
6+
7+

README.md

Lines changed: 2 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,2 @@
1-
# Sloopstash CTM App
2-
3-
Welcome to the **Sloopstash CTM app**, which includes both the backend and frontend components. This guide will walk you through the setup process to run the application locally.
4-
5-
## Project Structure
6-
7-
The project consists of two main folders:
8-
9-
- **backend**: Node.js backend API for the CTM app.
10-
- **frontend**: React frontend for the CTM app.
11-
12-
## Prerequisites
13-
14-
Before you start, ensure that you have the following installed:
15-
16-
- [Node.js](https://nodejs.org/) (v14 or later)
17-
- **Installation for Node.js**:
18-
- Visit the official [Node.js website](https://nodejs.org/) and download the latest stable version (LTS). Follow the installation instructions specific to your operating system (Windows, macOS, or Linux).
19-
- You can verify the installation by running the following commands in your terminal or command prompt:
20-
```bash
21-
node -v
22-
npm -v
23-
```
24-
If both commands return a version number, Node.js and npm (Node Package Manager) are successfully installed.
25-
26-
- [MongoDB](https://www.mongodb.com/) (running locally or remotely)
27-
- If you want to run MongoDB locally, install it from [MongoDB's official site](https://www.mongodb.com/try/download/community) and follow the instructions for your OS.
28-
- Alternatively, you can use [MongoDB Atlas](https://www.mongodb.com/cloud/atlas) for a cloud-based solution.
29-
30-
## Backend Setup
31-
32-
The backend of this application is built with **Node.js** and **Express**. Follow these steps to get the backend running locally.
33-
34-
### Steps:
35-
36-
1. **Navigate to the `backend` folder**:
37-
```bash
38-
cd sloopstash/backend
39-
```
40-
41-
2. **Create a `.env` file** for local configuration:
42-
```bash
43-
touch .env
44-
```
45-
46-
3. **Add the following environment variables** to the `.env` file:
47-
```env
48-
MONGO_URI=mongodb://localhost:27017/ctm
49-
JWT_SECRET=your_secret_key_here
50-
```
51-
52-
- `MONGO_URI`: The MongoDB connection string for your database.
53-
- `JWT_SECRET`: Secret key used to sign JWT tokens for authentication.
54-
55-
4. **Install dependencies**:
56-
```bash
57-
npm install
58-
```
59-
60-
5. **Start the backend server**:
61-
62-
- This command will use **nodemon** to run the server. Make sure you have **nodemon** installed globally if you don't have it already:
63-
```bash
64-
npm install -g nodemon
65-
```
66-
67-
```bash
68-
npm run dev
69-
```
70-
71-
The backend should now be running on [http://localhost:2000](http://localhost:2000).
72-
73-
74-
## Frontend Setup
75-
76-
The frontend is built with **React**. Follow these steps to get the frontend running locally.
77-
78-
### Steps:
79-
80-
1. **Navigate to the `frontend` folder**:
81-
```bash
82-
cd sloopstash/frontend
83-
```
84-
85-
2. **Create a `.env` file** for local configuration:
86-
```bash
87-
touch .env
88-
```
89-
90-
3. **Add the following environment variable** to the `.env` file:
91-
```env
92-
BACKEND_BASE_URL=http://localhost:2000/api
93-
```
94-
95-
- `BACKEND_BASE_URL`: The URL to the backend API. This allows the frontend to make API requests to the backend.
96-
97-
4. **Install dependencies**:
98-
```bash
99-
npm install
100-
```
101-
102-
5. **Start the frontend development server**:
103-
```bash
104-
npm start
105-
```
106-
107-
The frontend should now be running on [http://localhost:3000](http://localhost:3000).
108-
109-
## Additional Notes
110-
111-
- **MongoDB**: Make sure you have **MongoDB** running locally on your machine. If not, you can install it from [MongoDB's official site](https://www.mongodb.com/try/download/community) or use a cloud-based MongoDB service like [MongoDB Atlas](https://www.mongodb.com/cloud/atlas).
112-
113-
- **Development Setup**: The app is set up for local development. If you encounter any issues with missing dependencies or errors during setup, try deleting the `node_modules` folder and running `npm install` again.
114-
115-
- **Port Customization**: If you want to change the backend port or database URI, you can modify the `.env` files in both the frontend and backend directories accordingly.
116-
117-
- **Future Updates**: **Containerization** work using **Docker** is ongoing and will be available in future releases. Once containerization is complete, you will be able to run the app using Docker and Docker Compose.
118-
119-
## License
120-
121-
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
122-
123-
---
124-
125-
**Thank you for using the Sloopstash CTM app!**
1+
# Sloopstash CTM app,
2+
Welcome to the Sloopstash CTM app, which includes both the backend and frontend components. This guide will walk you through the setup process to run the containerized application.

init.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const express = require('express');
22
const cors = require('cors');
3-
require('dotenv').config();
3+
const path = require('path');
4+
require('dotenv').config();
45

56
const authRoutes = require('./controller/routes/auth_routes');
67
const contactRoutes = require('./controller/routes/contact_routes');
@@ -15,10 +16,21 @@ app.use(express.json());
1516
// Connect to MongoDB using the common module
1617
connectDB();
1718

18-
// Define your routes
19+
// Define routes for API
1920
app.use('/auth', authRoutes);
2021
app.use('/contacts', contactRoutes);
2122

23+
// Serve React build files
24+
if (process.env.NODE_ENV === 'production') {
25+
// Serve the static files from the React app (built version)
26+
app.use(express.static(path.join(__dirname, 'view/build')));
27+
28+
// The "catch-all" handler: when a request doesn't match any API routes, serve the React app
29+
app.get('*', (req, res) => {
30+
res.sendFile(path.join(__dirname, 'view/build', 'index.html'));
31+
});
32+
}
33+
2234
// Health check route
2335
app.get('/health', (req, res) => {
2436
res.status(200).json({

view/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
REACT_APP_API_BASE_URL=your_api_endpoint/api

0 commit comments

Comments
 (0)