This Node.js-based e-commerce platform is built using a RESTful API and designed to fulfill customer purchase requirements. It features user authentication tokens, secure user information, and testing capabilities. The code is written using TypeScript, unit testing, linting, and formatting best practices to ensure scalability and maintainability.
- Getting Started
- Installation & Dependencies
- Environment Variables
- Database & Server
- Usage Documentation
- Additional Notes
To get started with this project, follow these steps:
-
Make sure that you have Node.js and npm installed on your system. If you don't already have them, you can download and install them from the official Node.js website
-
Clone the repository:
https://github.com/RogerA11/Storefront-Backend.git -
Navigate to the root directory of the project:
cd Storefront-Backend -
The server is set up to listen on port number
3000. If you need to change the port number, you can do so in theapp.listen()function in thesrc/server.tsfile.
-
Open a terminal window and navigate to the root directory of your project.
-
Run the following command to install the dependencies listed in the package.json file:
npm install
This will install all the packages listed in the dependencies and devDependencies sections of the package.json file. The packages will be downloaded from npm and placed in a node_modules directory within the project.
These packages are required to run the project.
express: A web framework for Node.jssharp: An image processing librarysupertest: A library for testing HTTP APIstypescript: A typed superset of JavaScript
Installation example:
-
Open a terminal window and navigate to the root directory of your project.
-
Run the following command.
npm i express
The following environment variables need to be set up for the application to function:
- POSTGRES_HOST: The host name of the PostgreSQL server.
- POSTGRES_DB: The name of the development database.
- POSTGRES_TEST_DB: The name of the test database.
- POSTGRES_USER: The username used to connect to the database.
- POSTGRES_PASSWORD: The password used to connect to the database.
- ENV: The environment in which the application is running (either dev or test).
- BCRYPT_PASSWORD: A random password used for bcrypt hashing.
- SALT_ROUNDS: The number of salt rounds to use for bcrypt hashing.
- TOKEN_SECRET: A secret key used to sign JWT tokens.
To set these variables, create a .env file in the root directory of the project and add the variables with their respective values. An example .env file is provided in the repository as .env.example. Do NOT commit the .env file to version control. Instead, provide an example file and instruct users to create their own .env file with appropriate values.
Instructions for connecting to the Postgres database and running the server.
Database
- The
docker-compose.ymlfile defines a Docker Compose service for PostgreSQL. - Run the following command in the terminal to start the PostgreSQL service:
docker-compose up - Connect to a PostgreSQL database running on a Docker container:
psql -U postgresand insert passwordpostgres4343 - You are now connected to the database
Server
- To start the server run one of the following commands:
npm run startornpm run watch - The terminal should return
starting app on: 0.0.0.0:3000to indicate that the server is running
a Demo for running a RESTful route after successfully starting the server. The demo includes fictional data. Using a GET method on the products route with a specific id. This will return the product linked to the id
- Insert this link into a browser and run it:
http://localhost:3000It should display 'Hello, World!' - Add to the
/products/2to the link:http://localhost:3000/products/2 - If successful you should get a return similar to this:
{ "id": 2, "name": "Sapians", "price": 350, "category": "Books" }
For further details on various methods and routes. Refer to the REQUIREMENTS.md file.