This project is a backend server application that includes user authentication, file upload functionality, and sentiment analysis using a machine learning model. The server is built using Node.js, Express, and MongoDB. Sentiment analysis is performed using a Python backend with a scikit-learn model.
backend-server/
│
├── node_modules/
├── index.js
├── package.json
├── package-lock.json
│
├── python-backend/
│ ├── venv/
│ ├── app.py
│ ├── train_model.py
│ ├── sentiment_model.pkl
│ └── vectorizer.pkl
│
└── README.md
Node.js (v12 or higher)
MongoDB
Python (v3.6 or higher)
Postman (optional, for testing API endpoints)
git clone https://github.com/SHIV000000/Backend-Server
cd backend-server
npm install express mongoose multer jsonwebtoken bcrypt passport passport-jwt body-parser axios
cd python-backend
python -m venv venv
pip install Flask scikit-learn joblib
python train_model.py
python app.py
cd backend-server
Start the Node.js server:
node index.js
API Endpoints User Registration URL: /register Method: POST Request Body:
{
"username": "string",
"password": "string"
}
Response:
{
"message": "User registered successfully"
}
User Login URL: /login Method: POST Request Body:
{
"username": "string",
"password": "string"
}
{
"token": "string"
}
File Upload URL: /upload-file Method: POST
{
"Authorization": "Bearer <token>"
}
Form Data: file: File to be uploaded
{
"message": "File uploaded successfully"
}
URL: /analyze-sentiment Method: POST Request Body:
{
"text": "string"
}
{
"sentiment": "positive" | "negative"
}
Node.js (index.js) Dependencies: The necessary modules are imported, including Express, Mongoose, Multer, JWT, Passport, bcrypt, and Axios. MongoDB Connection: Connects to the MongoDB database. Passport JWT Strategy: Sets up JWT authentication using Passport. User Schema: Defines the user model schema with Mongoose. Routes: /register: Handles user registration. /login: Handles user login and JWT token generation. /upload-file: Handles file uploads, validating file type and size. /analyze-sentiment: Forwards text data to the Python backend for sentiment analysis. Server Setup: Starts the Express server on port 3000. Python (python-backend/app.py) Dependencies: Imports Flask, scikit-learn, joblib, and os. Model Loading: Loads the pre-trained sentiment analysis model and vectorizer. Routes: /analyze-sentiment: Accepts text data, processes it with the sentiment analysis model, and returns the result. Server Setup: Starts the Flask server on port 5000. Python (python-backend/train_model.py) Dependencies: Imports scikit-learn and joblib. Model Training: Loads training data and vectorizes it. Trains a Logistic Regression model for sentiment analysis. Saves the trained model and vectorizer to disk. Testing Using Postman Register a new user via the /register endpoint. Log in with the registered user via the /login endpoint and obtain the JWT token. Upload a file via the /upload-file endpoint using the obtained JWT token. Analyze sentiment via the /analyze-sentiment endpoint. Using curl
Register a new user:
curl -X POST -H "Content-Type: application/json" -d '{"username":"testuser", "password":"password"}' http://localhost:3000/register
curl -X POST -H "Content-Type: application/json" -d '{"username":"testuser", "password":"password"}' http://localhost:3000/login
curl -X POST -H "Authorization: Bearer <token>" -F "file=@path_to_your_file" http://localhost:3000/upload-file
Analyze sentiment:
curl -X POST -H "Content-Type: application/json" -d '{"text":"I love programming!"}' http://localhost:3000/analyze-sentiment
This project demonstrates a full-stack application integrating Node.js with a Python backend for machine learning. The Node.js server handles user authentication, file uploads, and delegates sentiment analysis to the Python backend, which uses a scikit-learn model for predictions. This setup provides a scalable architecture for incorporating advanced machine learning models into web applications.