This is the sherehe backend for academia.
This project provides a backend API for Events in academia using Express.js and PostgreSQL.
- RESTful API with CRUD support
- PostgreSQL for relational data storage
- MVC-style API project structure
- Unit testing
- CORS and dotenv integration
- Runtime: Node.js.
- Framework: Express.js
- Database: PostgreSQL
- Env Handling: dotenv
- Others: CORS
- Node.js >= 14.x
- PostgreSQL installed and running
git clone https://github.com/opencrafts-io/sherehe.git cd sherehe
npm install
Create a .env file in the root and add:
- vist the env documentation at the end
Create your database manually or via CLI: just remember your password and username then insert the nessary in the .env
createdb sherehe
npx node-pg-migrate up
npm run dev
npm test (on a different terminal on the same folder)
Scripts
npm run dev # Start server with nodemon node index.js # Start server in production mode
You should see something like this:
- Welcome to Sherehe Backend
(Note) it runs on port 3000
- This is where middleware such as cors are implemented
- This is also where urls first segment are declared
- This is where subsequent segments of a url are assigned to a controller
- This is where information from the user is recieved and any conditions are set so as to pass clean data to the models
- This is also where responses and their corresponding statuses are return back to the user not forgetting the data returned back to the user
- It goes without saying that this is where each model is given to a particular controller
- Responses must be returned with their correct status
- This recieves data from the controller and performs either create , update , delete or get values on the table
- The model can only return message whether a values on a table have been changed , deleted or inserted . It should return data only or valid reponse messages
- This is where tables are given life and structure.
- The name of the file example (1751445846272_create-events-table.js) shows which table is in structured in the file to create such a table run the command:
-
- npx node-pg-migrate create create-(name_of_the_table)_-table
to run migration
-
- npx node-pg-migrate up
-
- npx node-pg-migrate down (rollback)
- In some instances you may have to drop the intended tables if rollback does not work
(Note) The database to be used must be postgres
- This project only uses camel case when naming variable , functions
- The project uses "type": "module" which mainly uses keyword import during importation rather than require which is the default
-
I do realise that I have repeated the credentials in database and migrations just bear with me
-
Database DB_USER= * (Your username) DB_HOST=db DB_NAME=sherehe DB_PASSWORD= * (Your personal postgres database password) DB_PORT=5432
-
Migrations PGHOST=db PGUSER=* (Your username) PGPASSWORD= * (Your personal postgres database password) PGDATABASE=sherehe PGPORT=5432
- This project uses jest for tests
- The tests are in the folder tests where you can see test for every model , controller or even route
- The babel.config.js helps in making sure that jest knows we are using type: module
- This options are provided by jest
- Watch Usage
-
- Press f to run only failed tests.
-
- Press o to only run tests related to changed files.
-
- Press p to filter by a filename regex pattern.
-
- Press t to filter by a test name regex pattern.
-
- Press q to quit watch mode.
-
- Press Enter to trigger a test run.
-
- Jest is like nodemon it waits for any change of code then runs the tests according to what it has changed
- When you first clone the repo no test will be done since thier is no change of code , so press a to start the tests
- Errors under console.log are fine and intentionally induced and dislpayed
- To run tests
-
- npm test