A RESTful API backend built with Node.js and Express.js, designed to serve a Flutter application. This API provides authentication (including OAuth), post management with image uploads, and commenting functionality using Prisma ORM with MySQL database.
- User Authentication
- Local Authentication (Register/Login)
- Google OAuth Integration
- GitHub OAuth Integration
- JWT-based Authorization
- Post Management
- CRUD operations
- Multiple Image Upload Support (up to 5 images per post)
- Commenting System
- Author Information
- Database & Storage
- MySQL Database Integration
- Prisma ORM for Database Operations
- Local File Storage for Images
- Security
- JWT Token Authentication
- Password Hashing with bcryptjs
- File Upload Validation
- User Authorization Checks
- Node.js (v14 or higher)
- MySQL Server
- npm or yarn package manager
- Google OAuth credentials (for Google authentication)
- GitHub OAuth credentials (for GitHub authentication)
- Runtime Environment: Node.js
- Framework: Express.js
- Database: MySQL
- ORM: Prisma
- Authentication:
- JWT (JSON Web Tokens)
- Passport.js (OAuth)
- File Upload: Multer
- Password Hashing: bcryptjs
-
Clone the repository:
git clone https://github.com/bellandry/simple-nodejs-api.git cd simple-nodejs-api -
Install dependencies:
npm install
-
Set up environment variables: Create a
.envfile in the root directory and add the following variables:DATABASE_URL="mysql://username:password@localhost:3306/your_database" JWT_SECRET="your-jwt-secret" JWT_REFRESH_SECRET="your-jwt-refresh-secret" PORT=3000 GOOGLE_CLIENT_ID="your-google-client-id" GOOGLE_CLIENT_SECRET="your-google-client-secret" GITHUB_CLIENT_ID="your-github-client-id" GITHUB_CLIENT_SECRET="your-github-client-secret" SESSION_SECRET="your-session-secret"
-
Run Prisma migrations:
npm run db:push
-
Create uploads directory:
mkdir uploads
Development mode with auto-reload:
npm run devProduction mode:
npm startPOST /api/auth/register- Register a new userPOST /api/auth/login- Login userGET /api/auth/google- Google OAuth loginGET /api/auth/google/callback- Google OAuth callbackGET /api/auth/github- GitHub OAuth loginGET /api/auth/github/callback- GitHub OAuth callback
GET /api/posts- Get all posts with images and commentsGET /api/posts/:id- Get a specific post with images and commentsPOST /api/posts- Create a new post with images- Accepts multipart/form-data with:
- title: Post title
- content: Post content
- images: Up to 5 image files
- Accepts multipart/form-data with:
POST /api/posts/:id/comments- Add a comment to a postDELETE /api/posts/:id- Delete a post and its associated images
flutter-nodejs-api/
├── src/
│ ├── config/
│ │ ├── oauth.js
│ │ └── passport.js
│ ├── controllers/
│ │ ├── authController.js
│ │ └── postController.js
│ ├── middleware/
│ │ ├── authMiddleware.js
│ │ └── uploadMiddleware.js
│ ├── models/
│ │ └── Post.js
| | └── User.js
│ ├── routes/
│ │ ├── authRoutes.js
│ │ └── postRoutes.js
│ ├── app.js
│ └── server.js
├── prisma/
│ └── schema.prisma
├── uploads/ # Image storage directory
├── .env
├── package.json
└── README.md
- @prisma/client: ^5.22.0
- bcryptjs: ^2.4.3
- dotenv: ^16.4.5
- express: ^4.21.1
- jsonwebtoken: ^9.0.2
- multer: Latest version
- passport: Latest version
- passport-google-oauth20: Latest version
- passport-github2: Latest version
- express-session: Latest version
-
OAuth Setup:
- Keep your OAuth credentials secure
- Use environment variables for sensitive data
- Set up proper callback URLs in Google and GitHub developer consoles
-
File Uploads:
- Maximum file size: 5MB per image
- Allowed formats: jpg, jpeg, png, gif
- Files are stored in the
uploadsdirectory
-
Authentication:
- JWT tokens expire after 24 hours
- Passwords are hashed using bcryptjs
- Session management for OAuth
The API returns appropriate HTTP status codes:
- 200: Success
- 201: Resource created
- 400: Bad request
- 401: Unauthorized
- 403: Forbidden
- 404: Not found
- 500: Server error
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Create a new Pull Request
- Built with ❤️ by Landry Bella
This project is licensed under the MIT License - see the LICENSE file for details.