A robust shared code service built with NestJS, Prisma, and MongoDB. This backend API provides a complete paste/code sharing platform with advanced features for managing code snippets.
- Create, read, update, and delete pastes
- Password protection for pastes
- Expiration dates for pastes
- Public/private visibility settings
- View counter tracking
- Pagination support
- Author IP tracking
- Automatic cleanup of expired pastes
- Swagger API documentation
- Custom logger module
- TypeScript support with strict typing
- Comprehensive testing setup
- Node.js (v16 or higher)
- MongoDB database (local or MongoDB Atlas)
- Yarn package manager
-
Clone and install dependencies
yarn install
-
Configure environment
cp env-example .env
Edit
.envand set your MongoDB connection string:DATABASE_URL="mongodb://localhost:27017/sharedcode" -
Setup database
yarn migrate
-
Start development server
yarn dev
-
Test the API
- The server will start on http://localhost:3000
- API documentation is available at http://localhost:3000/docs (Swagger UI)
Create a paste:
curl -X POST http://localhost:3000/pastes \ -H "Content-Type: application/json" \ -d '{ "title": "Hello World", "content": "console.log(\"Hello, World!\");", "language": "javascript" }'
yarn dev- Start in development mode with hot reloadyarn start- Start the applicationyarn build- Build the application for productionyarn start:prod- Start in production mode
yarn migrate- Run database migrations and generate Prisma clientyarn generate- Generate Prisma clientyarn db:dev:console- Open Prisma Studio for database managementyarn db:dev:migrate- Push database schema and generate clientyarn db:test:console- Open Prisma Studio for test databaseyarn db:test:migrate- Push test database schema and generate client
yarn lint:check- Check for linting errorsyarn lint:fix- Fix linting errors automaticallyyarn format:check- Check code formattingyarn format:fix- Fix code formattingyarn format- Format all TypeScript filesyarn type-check- Run TypeScript type checking
yarn test- Run unit testsyarn test:watch- Run tests in watch modeyarn test:cov- Run tests with coverage reportyarn test:e2e- Run end-to-end testsyarn test:debug- Run tests in debug mode
src/
βββ paste/ # Paste module
β βββ dto/ # Data Transfer Objects
β β βββ create-paste.dto.ts
β β βββ update-paste.dto.ts
β β βββ index.ts
β βββ response/ # Response DTOs
β β βββ paste-response.dto.ts
β β βββ paste-list-response.dto.ts
β β βββ paginated-response.interface.ts
β β βββ index.ts
β βββ docs/ # API documentation
β β βββ swagger.constants.ts
β β βββ api-examples.ts
β β βββ error-responses.ts
β β βββ index.ts
β βββ paste.controller.ts # REST API endpoints
β βββ paste.service.ts # Business logic
β βββ paste.module.ts # Module definition
β βββ README.md # Paste module documentation
βββ prisma/ # Database module
β βββ prisma.service.ts # Prisma client service
β βββ prisma.module.ts # Prisma module
βββ app.config.ts # Application configuration
βββ app.module.ts # Main application module
βββ main.ts # Application entry point
libs/
βββ logger/ # Custom logger library
βββ src/
βββ logger.service.ts
βββ logger.module.ts
βββ logger.middleware.ts
βββ index.ts
prisma/
βββ schema.prisma # Database schema definition
dist/ # Compiled JavaScript output
test/ # E2E tests
Local MongoDB:
DATABASE_URL="mongodb://localhost:27017/sharedcode"
MongoDB with authentication:
DATABASE_URL="mongodb://username:password@localhost:27017/sharedcode?authSource=admin"
MongoDB Atlas:
DATABASE_URL="mongodb+srv://username:password@cluster.mongodb.net/sharedcode?retryWrites=true&w=majority"
Create a .env file based on env-example with the following variables:
DATABASE_URL: MongoDB connection string (required)PORT: Server port (optional, defaults to 3000)ENVIRONMENT: Environment mode (development/production)ENABLE_LOGGER: For enabling logger libs to log all events on terminal
Once the server is running, you can access the interactive API documentation at:
- Local: http://localhost:3000/docs
- Development: Your development URL +
/docs
The Swagger documentation includes:
- All available endpoints
- Request/response schemas
- Example requests and responses
- Authentication requirements
- Error response formats
You can test the API using:
- Swagger UI - Built-in interactive documentation at
/docs - Postman - Import endpoints from Swagger documentation
- cURL - Command line testing (examples provided above)
- Thunder Client - VS Code extension
- Insomnia - API client
This project uses:
- ESLint for linting
- Prettier for code formatting
- TypeScript for type safety
Run yarn lint:fix and yarn format:fix before committing.
The project is configured to run linting and formatting checks before commits.
yarn build
yarn start:prod- Set
ENVIRONMENT=production - Configure production MongoDB connection
- Ensure all required environment variables are set
The application can be containerized using Docker. A Dockerfile is recommended for production deployments.
Database Connection Failed
- Verify MongoDB is running
- Check
DATABASE_URLin your.envfile - Ensure database user has proper permissions
Port Already in Use
- Change the port in your
.envfile - Kill the process using the port:
lsof -ti:3000 | xargs kill
Prisma Issues
- Run
yarn generateto regenerate Prisma client - Run
yarn migrateto sync database schema
TypeScript Errors
- Run
yarn type-checkto see detailed type errors - Ensure all dependencies are installed:
yarn install
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests and linting
- Submit a pull request
This project is licensed under UNLICENSED.