The backend of Thunder is a monolithic Nodejs application implemented with the Koajs 1.x framework. It has the following features:
app.js
is the starting point of the server, and serves as a directory for the HTTP endpoints.
Most functionality is split into task specific modules. Generally this is a 1 to 1 relationship with collections in the database. These module files exist in the root directory of the repo (little messy). The modules contain the REST endpoints and most of the business logic / database interactions.
Any outgoing information besides HTTP responses goes through the Broadcast
module which sends messages through socket.io to all logged in users.
After cloning:
Create a serverConfig.js
in the root folder with the contents:
Then run.
You can update the frontend with gulp:
Typically,
this.data
object (for PUT
& POST
), and/or in the this.params
object (for GET
& DELETE
).this.decodedToken
also may have relevant data. Some user specific parameters - username
, facility
, role
, flags
, etc. are inside the JWT and are passed with each authenticated request.this.body
with the HTTP status in this.status
Auth typically follows this flow:
hash
and salt
and if successful, they are issued a JWTflags
in the JWT and database) and an expiration date.Authorization
header (without the Bearer
syntax)The authed
here will make sure that the JWT with the request is both valid (issued by the server) and not expired. So any logged in user will be able to access a user via a username.
Here, the has
middleware will check that the JWT both is valid, and contains the managePasswords
flag. So only users with the managePasswords
permision can reset a password.
The authentication code exists mostly in auth.js
and user.js
.
The idea behind broadcasting would that only certain people would want to listen to certain messages. To this end, messages sent from the server would have a structure like:
but generally in practice the for
field was ignored and was often just {}
.
On the client side, a module would listen for broadcasts with: