This project is an Express application with MongoDB integration, featuring authentication using Passport.js with both local and Discord strategies. It also includes comprehensive testing using Jest for unit tests and SuperTest for end-to-end tests.
- User authentication using Passport.js with local and Discord strategies.
- MongoDB integration for storing user data.
- Unit testing with Jest for individual components.
- End-to-end testing with SuperTest for API endpoints.
- Express middleware for authentication and error handling.
Before running the project, make sure you have the following installed:
- Node.js and npm (or yarn)
- MongoDB (local or remote)
- Discord Developer Account (for setting up OAuth2)
-
Clone the repository to your local machine:
git clone https://github.com/petprog/express-mongo-test.git
-
Install dependencies:
cd your-project-directory npm install
-
Set up environment variables:
Create a
.env
file in the root directory and configure the following variables:DATABASE_URI=mongodb://localhost:27017/your-database-name SESSION_SECRET=your-session-secret DISCORD_CLIENT_ID=your-discord-client-id DISCORD_CLIENT_SECRET=your-discord-client-secret DISCORD_CALLBACK_URL=your-discord-callback-url COOKIE_PARSER_SECRET=your-cookie-parsr-secret
Adjust the values based on your configuration.
-
Start the server:
npm start
-
Visit
http://localhost:3000
in your browser to access the application.
For local authentication, users can register with their username and password.
Users can also log in using their Discord account. This requires setting up a Discord application in the Discord Developer Portal.
To run the unit tests:
npm run test
To run the end-to-end tests:
npm run test:e2e
- Unit Tests: Write unit tests using Jest for individual functions, controllers, and middleware.
- End-to-End Tests: Use SuperTest to write end-to-end tests for API endpoints, ensuring the entire application functions correctly.
- config/: Contains configuration files MongoDB.
- controllers/: Includes controller functions for handling authentication and other routes.
- utils/: Custom middleware functions for authentication and error handling.
- mongoose/schema/: MongoDB schema models.
- routes/: Express route handlers.
- tests/: Contains unit tests
- e2e/: and end-to-end tests.
- utils/: Utility functions used across the application, Custom middleware functions for authentication and error handling
- index.js: Entry point of the application.
Contributions are welcome! Feel free to submit issues or pull requests for new features, bug fixes, or improvements.
This project is licensed under the MIT License - see the LICENSE file for details.