This is a simple JSON Server backend for a book library application, built using Node.js and the json-server
package.
- Serves data from a
db.json
file. - Includes middleware to disable CORS, allowing requests from any origin.
- Uses
morgan
for request logging. - Configurable port via
.env
file or defaults to 5005.
-
Install dependencies:
npm install json-server morgan dotenv
-
Create a
.env
file (optional) to configure the port:PORT=5005
-
Create a
db.json
file with your data. Example:{ "books": [ { "id": 1, "title": "The Great Gatsby", "author": "F. Scott Fitzgerald" }, { "id": 2, "title": "To Kill a Mockingbird", "author": "Harper Lee" } ] }
-
Start the server:
node app.js
or
npm start
-
Access the data via the following endpoints:
GET /books
- Get all booksGET /books/:id
- Get a specific book by IDPOST /books
- Create a new bookPUT /books/:id
- Update an existing bookDELETE /books/:id
- Delete a book
json-server
: For creating a REST API from a JSON file.morgan
: For request logging.dotenv
: For loading environment variables from a.env
file.