This is a basic boilerplate for creating a simple API using Node.js and Express. It includes a protected route with an API key and a sample non-protected route. The repository is intended to be used as a template for creating new projects.
-
Clone this repository:
git clone https://github.com/wilburforce83/simple-api-boilerplate.git
-
Navigate into the cloned directory:
cd simple-api-boilerplate
-
Install dependencies:
npm install
-
Create a
.env
file in the root directory of the project. -
Add the following environment variables to the
.env
file:PORT=3000 API_KEY=your_api_key_here
Replace
your_api_key_here
with your desired API key. -
Ensure to add the
.env
file to.gitignore
to prevent sensitive information from being committed to version control.
To start the server, run:
npm start
-
Unprotected Route:
- URL:
/data
- Method:
GET
- Description: Retrieve all data stored in
/db/db.json
(unprotected). - Note: This route is currently commented out in
server.js
.
- URL:
-
Protected Route:
- URL:
/auth/data
- Method:
GET
- Description: Retrieve data with API key protection.
- API Key Required: Yes
- URL:
server.js
: Main entry point of the application.db/
: Folder containing the JSON database file (db.json
).example/
: Folder containing an HTML page with examples of how to request data from the API.
- This repository is a public template and can be used and modified freely.
- The API key is required for accessing protected routes.
- The file
/b/db.json
contains a large object of demo data sourced from https://github.com/json-iterator/test-data/blob/master/large-file.json for you to play around with until you have your own data to work with.
This project is licensed under the GNU General Public License v2.0 (GNU GPLv2) - see the LICENSE file for details.