A full-stack web application for managing a library of books with user authentication.
- User authentication with JWT tokens
- Add, edit, delete, and view books
- Secure password hashing with bcryptjs
- RESTful API with Express.js
- MongoDB for data persistence
- Responsive web interface
- Node.js (v14 or higher)
- MongoDB (local or cloud instance)
- npm (comes with Node.js)
-
Clone the repository
git clone https://github.com/PavaniP03/Online-Library-Management-system.git cd Online-Library-Management-system -
Install dependencies
npm install
-
Set up environment variables
cp .env.example .env
Edit
.envand configure:MONGODB_URI=mongodb://localhost:27017/online-library JWT_SECRET=your_super_secret_key_change_this_in_production PORT=3000 NODE_ENV=development -
Create a test user (optional)
Edit
createUser.jswith desired credentials and run:node createUser.js
Or create through the API after starting the server.
-
Add sample books (optional)
Run to populate database with sample books:
node insertBooks.js
npm startThe application will be available at http://localhost:3000
- Username:
yourusername - Password:
yourpassword
.
├── server.js # Express server and API routes
├── public/
│ ├── index.html # Frontend HTML
│ ├── scripts.js # Frontend JavaScript
│ └── styles.css # Frontend CSS
├── createUser.js # Utility to create test users
├── insertBooks.js # Utility to insert sample books
├── package.json # Dependencies
├── .env.example # Environment variables template
├── .gitignore # Git ignore rules
└── README.md # This file
- POST
/api/login- Login with username and password- Body:
{ "username": "user", "password": "pass" } - Returns:
{ "username": "user", "token": "jwt_token" }
- Body:
All book endpoints require the Authorization: Bearer <token> header.
- GET
/api/books- Get all books - POST
/api/books- Add a new book- Body:
{ "title": "Book Title", "author": "Author Name" }
- Body:
- PUT
/api/books/:id- Update a book- Body:
{ "title": "New Title", "author": "New Author" }
- Body:
- DELETE
/api/books/:id- Delete a book
- Create a Heroku app:
heroku create your-app-name - Add MongoDB Atlas URI:
heroku config:set MONGODB_URI="your_atlas_uri" - Set JWT secret:
heroku config:set JWT_SECRET="your_secret_key" - Deploy:
git push heroku main
- Set environment variables on the hosting platform
- Ensure MongoDB is accessible
- Run
npm install && npm start
- Change
JWT_SECRETto a strong, unique key - Use MongoDB Atlas or another cloud database service
- Set
NODE_ENV=production - Enable HTTPS
- Set
FRONTEND_URLenvironment variable for CORS - Add input validation and sanitization
- Implement rate limiting
- Add database backups
- Monitor application logs
- JWT tokens expire after 24 hours
- Passwords are hashed using bcryptjs
- CORS is configured for security
- Environment variables should never be committed
- Ensure MongoDB is running locally or provide a valid MongoDB URI
- Check
MONGODB_URIin.env
- Clear browser localStorage:
localStorage.clear() - Restart the server
- Verify
JWT_SECRETmatches between frontend and backend
- Change
PORTin.env - Or kill process using the port
ISC
PavaniP03
For issues or questions, please open a GitHub issue.