test-backend-vanilla
Prerequisites
- MySQL for storage
- Mocha for testing
Configurations
The configurations to set up the server & database can be accessed or modified in ./config.js.
Server
port: Server port (optional; defaults to 80)pkeyFile: Private key file to enable https (optional)certFile: Certificate file to enable https (optional)
Database
host: MySQL hostuser: MySQL userpassword: MySQL passworddatabase: MySQL database name (database have already been created via thecreate database [name]SQL command)
Getting started
Once the database has been created and the configurations (eg. the database name) have been set correctly, then
- Run
npm installfrom the local directory - Run
npm startto fire up the server ORnpm testto run the tests
Interacting with the User model
The user model is defined in ./api/user.js and can be interacted with via a simple REST API that supports basic CRUD operations:
- Create:
POST /user- Accepts JSON that specifies the following properties:
namedobaddressdescription
- Returns the new user's id
- Accepts JSON that specifies the following properties:
- Read:
GET /user/:id- Accepts the user id as a URL parameter
- Returns JSON with the following properties populated from the database
idnamedobaddressdescriptioncreated_at
- Update:
PUT /user/:id- Accepts the user id as a URL parameter
- Accepts JSON that specifies any of the following properties:
namedobaddressdescription
- Does not return data
- Delete:
DELETE /user/:id- Accepts the user id as a URL parameter
- Does not return data
Extending models and routes
Any additional models along with routes & handlers to change data reflected on MySQL can be declared in the ./api directory, along the same lines that the ./api/user.js module has been specified. Once a new such module has been declared, it must be registered in ./main.js.
For example, the ./api/user.js module serves two actions:
- Defines the user model as a MySQL table with appropriate fields
- Registers the REST routes and handlers to create, access, and modify user data.
Then, ./main.js is where the ./api/user.js module is registered, by calling require('./api/user.js').
For each module declared in ./api, test cases may be written in ./tests.
Directory structure
./config.js: Server and database configurations./main.js: App entrypoint./lib: Server and Database routines./lib/io.js: Network I/O module, managing server lifecycle & routing./lib/db.js: MySQL database module, abstracting access to database
./api: Models & routing./api/user.js: Declares user model and appropriate REST routes to access it
./tests: Tests of modules in./api./tests/user.js: Test cases on the user model declared in./api/user.js