This is the server and API for the Bookish mobile app. It is built with TypeScript, MongoDB, Node.js, and Express. This API is responsible for managing user accounts and the books they have added to their bookshelves. While the public Google Books API is used for searching and retrieving book information, this API handles the user-specific functionality.
- TypeScript
- MongoDB
- Node.js
- Express
- Mongoose (for defining schemas)
The Book
schema defines the structure of the books stored in the database. Here is an overview of the Book schema:
const BookSchema: Schema = new Schema({
title: { type: String, required: true },
author: { type: String, required: true },
bookshelf: { type: String, required: true }, // bookshelf name (read, currently reading, want to read)
owned: { type: Boolean, default: false }, // to indicate if user owns the book
dateRead: { type: Date }, // date user finished reading book
rating: { type: Number }, // user's rating (1-5)
review: { date: { type: Date }, text: { type: String } }, // user's review
volumeId: { type: String, required: true }, // google books api volume id (for fetching book info)
userId: { type: ObjectId, ref: 'User' }, // link user to book
})
title
(required): The title of the book, used for searching in bookshelves.author
(required): The author of the book, used for searching in bookshelves.volumeId
(required): The Google Books API volume ID for fetching book information.userId
: References the User model and links the book to a specific user.bookshelf
(required): The name of the bookshelf where the book is categorized (read, currently reading, want to read).owned
: Indicates whether the user owns the book (default is false).read
: The date when the user finished reading the book (can be used to show reading history).rating
: The user's rating for the book (ranging from 1 to 5).review
: The user's review, including the review date and text.
- Clone the repository:
git clone https://github.com/v-prt/bookish-backend.git
- Navigate to the project directory:
cd bookish-backend
- Install dependencies:
npm install
-
Create a
.env
file in the root directory of the project. -
Set the following environment variables in the
.env
file:MONGO_URI=<your-mongodb-uri> TOKEN_SECRET=<your-generated-key> PORT=<your-server-port>
Replace
<your-mongodb-uri>
with your MongoDB connection URI,<your-generated-key>
with the generated token secret, and<your-server-port>
with your desired port number for the server. See below for more details on getting your own MongoDB URI and token secret.Make sure to keep sensitive information secure and avoid sharing them publicly or committing them to version control.
The MONGO_URI
environment variable specifies the connection URI for your MongoDB database. Follow the steps below to obtain your MongoDB URI:
-
Create a MongoDB Atlas account or access your existing account.
-
Create a new project or select an existing project.
-
Navigate to the "Clusters" section and click on "Connect" for your desired cluster.
-
Choose "Connect your application" as the connection method.
-
Select the appropriate driver (e.g., Node.js) and version.
-
Copy the provided connection string (the MongoDB URI).
Example MongoDB URI format:
mongodb+srv://<username>:<password>@<cluster-address>/<database-name>?retryWrites=true&w=majority
-
Replace
<username>
,<password>
,<cluster-address>
, and<database-name>
with your actual credentials and database information. -
Set the
MONGO_URI
environment variable in your development environment or deployment platform to the obtained MongoDB URI.-
For local development, paste the value in the
.env
file as shown above. -
For deployment, consult your deployment platform's documentation on how to set environment variables. Set the
MONGO_URI
variable to the obtained MongoDB URI value.
-
-
Save the changes and restart your server for the new configuration to take effect.
The TOKEN_SECRET
environment variable is required for signing JWT tokens in order to authenticate users. Follow the steps below to generate a secure key:
-
Open a terminal or command prompt.
-
Run the following command to generate a random key:
node -e "console.log(require('crypto').randomBytes(64).toString('hex'));"
This command generates a random 64-byte (512-bit) secret key and outputs it as a hexadecimal string.
-
Copy the generated key.
-
Set the
TOKEN_SECRET
environment variable in your development environment or deployment platform.-
For local development, paste the generated token secret in the
.env
file as shown above. -
For deployment, consult your deployment platform's documentation on how to set environment variables. Set the
TOKEN_SECRET
variable to the generated token secret value.
-
-
Save the changes and restart your server for the new configuration to take effect.
Now the necessary environment variables are properly set, allowing your backend to connect to MongoDB, sign/verify JWT tokens securely, and listen on the specified port.
- Start the server:
npm run start:dev
The client for Bookish is available in the bookish-frontend repository. Once you have your backend running, you may set up and run the frontend application.
Contributions are welcome! If you would like to contribute to the Bookish backend, please follow these steps:
- Fork the repository.
- Create a new branch for your feature/bug fix.
- Commit your changes.
- Push the branch to your forked repository.
- Open a pull request with a detailed description of your changes.
Please ensure that your code adheres to the existing code style and includes appropriate tests.
This project is licensed under the MIT License. See the LICENSE file for details.