How to Set Up and Run the Bank API Server with Express: A Complete Guide
You can set up and run the Bank API server with Express by navigating to the 7-bank-project/api directory, running npm install to install dependencies, and executing npm start to launch the in-memory REST service on port 5000.
The Bank API is a lightweight, educational Node.js service built with Express.js that demonstrates RESTful backend patterns for a personal budgeting application. Located in the microsoft/Web-Dev-For-Beginners repository, this server provides complete CRUD operations for accounts and transactions without requiring a database connection. This guide walks you through the exact implementation details found in the source code to get the server running locally.
Prerequisites and Project Structure
Before starting, ensure you have Node.js installed on your machine. The API code resides in the 7-bank-project/api folder of the repository and consists of three key files:
server.js– Main application logic, route definitions, and in-memory data storepackage.json– Project metadata, dependency list, and npm scriptsREADME.md– Quick-start documentation for the API module
Understanding the Express Architecture
In server.js, the Express application initializes on line 27 with middleware configuration for body-parser and cors on lines 28-31. The server uses an in-memory JavaScript object (db) defined on lines 11-24 to store account data and transaction arrays. This architecture makes the API ideal for learning environments but unsuitable for production workloads requiring data persistence.
Installing Dependencies and Configuration
Navigate to the API directory and install the required packages including Express, body-parser, and cors as specified in the dependencies section of package.json.
cd 7-bank-project/api
npm install
The server configuration uses process.env.PORT with a fallback to port 5000 (line 8). The CORS policy restricts origins to localhost or 127.x.x.x addresses (line 30) for security during local development.
Starting the Bank API Server
Launch the server using the provided npm script or Node directly:
npm start
# or
node server.js
Upon successful startup, the console outputs "Server listening on port 5000" as implemented on line 96 of server.js. The application mounts all routes under the /api prefix, added on line 91.
Testing the API Endpoints
Verify the installation by querying the root information endpoint:
curl http://localhost:5000/api/
This returns the package description and version as configured on lines 38-41 of server.js.
Creating and Managing Accounts
Create a new account using the POST endpoint with JSON payload validation for user and currency fields (lines 45-77):
curl -X POST http://localhost:5000/api/accounts \
-H "Content-Type: application/json" \
-d '{"user":"alice","currency":"$","description":"Alice budget","balance":100}'
Retrieve specific account data (lines 81-91):
curl http://localhost:5000/api/accounts/alice
Remove an account permanently (lines 95-108):
curl -X DELETE http://localhost:5000/api/accounts/alice
Handling Transactions
Add a transaction to an existing account, which generates an MD5 ID and updates the account balance (lines 112-161):
curl -X POST http://localhost:5000/api/accounts/alice/transactions \
-H "Content-Type: application/json" \
-d '{"date":"2024-10-01","object":"Salary","amount":2500}'
Delete a specific transaction using the ID returned from the previous call (lines 165-187):
curl -X DELETE http://localhost:5000/api/accounts/alice/transactions/<tx-id>
Summary
- The Bank API server requires running
npm installin the7-bank-project/apidirectory to install Express and middleware dependencies. - Start the server with
npm start(which executesnode server.js) on port 5000 by default, or specify a custom port via thePORTenvironment variable. - The in-memory data store (
dbobject on lines 11-24 ofserver.js) persists accounts and transactions only while the Node.js process remains active. - All routes are prefixed with
/apiand include CRUD operations for accounts and nested transaction resources. - CORS restrictions on line 30 limit access to localhost origins, ensuring secure local development.
Frequently Asked Questions
What port does the Bank API server use by default?
The server listens on the port specified in the PORT environment variable or defaults to 5000 as defined on line 8 of server.js. You can override this by setting the environment variable before startup, for example: PORT=3000 npm start.
Does the Bank API persist data to a database?
No. According to the source code in server.js (lines 11-24), the API uses a simple in-memory JavaScript object called db to store account data and transaction arrays. This data disappears when the server stops, making it perfect for learning but requiring a proper database for production use.
How do I enable external access to the Bank API?
By default, the CORS middleware configured on line 30 restricts requests to localhost or 127.x.x.x addresses. To allow external access, you would need to modify the CORS configuration object in server.js, though this is not recommended without additional authentication for this educational project.
Where are the API route handlers defined?
All route handlers are defined in 7-bank-project/api/server.js starting from line 38, including GET /api/ for server info, POST /api/accounts for account creation, and nested transaction routes for adding and removing financial records. The Express application applies the /api prefix to all routes on line 91.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →