waw-sem is the server engine module for the waw platform. It wires together an Express HTTP server, optional MongoDB connection + session middleware, Socket.IO transport, and a convention-driven CRUD generator. Sem exposes its runtime API through the shared waw context so other modules can contribute backend behavior by adding *.collection.js, *.api.js, and/or module.crud configuration.
When server/sem/index.js runs, it performs these steps in order:
- Initializes Express + HTTP server (
util.express) - Initializes MongoDB + sessions (
util.mongo)- Connects Mongoose if
waw.config.mongois configured - Always installs
express-sessionmiddleware
- Connects Mongoose if
- Initializes Socket.IO (
util.socket) - Initializes CRUD engine (
util.crud) - Loads every module file ending with
collection.js(in module order) - Loads every module file ending with
api.js(in module order) - Calls
waw.crud.finalize()to register CRUD endpoints from module configs - Starts listening on
waw.config.port(defaults to8080)
util.express.js creates and exposes:
waw.app— Express appwaw.server— Node HTTP server created from the Express appwaw.express— Express exportwaw.router(basePath)— mounts an Express Router atbasePathand returns it
Built-in routes / middleware:
GET /status— returns HTTP 200 withtruecookie-parsermethod-override("X-HTTP-Method-Override")- Optional favicon if
waw.config.iconpoints to an existing file
Auth helpers:
waw.ensure(req,res,next)— requiresreq.useror responds withwaw.resp(false)waw.role(roles, middleware?)— role gating based onreq.user.is[role], responds401withfalseon failurewaw.nextandwaw.blockconvenience helpers
Note:
util.expressdefineswaw.resp = (body) => bodyas a minimal default; CRUD useswaw.resp(...)when sending JSON responses.
util.mongo.js sets:
waw.mongoose— Mongoose instancewaw.mongoUrl— resolved Mongo connection string when Mongo is configuredwaw.store— session store instance when Mongo is configured (Connect-Mongo), otherwiseundefined
Mongo configuration behavior:
- If
waw.config.mongois a string, it is treated as the full Mongo URI. - If it is an object, Sem supports building a URI from keys such as
srv,uri,host/hosts,port,user/pass,db, and optional query options (viaoptionsobject or top-level fields likereplicaSet,authSource,readPreference, etc.). - If
waw.mongoUrlexists and Mongoose is not connected, Sem connects and logs basic connection events.
Sessions behavior:
- Installs
express-sessionmiddleware. - Cookie max age defaults to one year unless
waw.config.sessionis a number. - Cookie
domainuseswaw.config.domainwhen provided. - Session name is
express.sid.<prefix>where<prefix>iswaw.config.prefixor empty. - Secret rotation is maintained in the project’s
server.jsonundersecretKeysas an array of{ key, createdAt }.- A new secret is generated if there is no secret or the newest secret is older than one week.
- Up to 5 recent secrets are kept to allow rotation without breaking existing sessions.
util.socket.js creates a Socket.IO server on waw.server and exposes:
waw.socket.io— Socket.IO server instancewaw.socket.emit(event, payload, room?)— emits globally or to a roomwaw.socket.add(fn)— adds a connection handler(socket) => { ... }
Defaults:
- CORS allows any origin (
origin: "*") - Transports:
websocketandpolling - On connection, a default handler forwards
create,update,unique,deleteevents to all other clients viasocket.broadcast.emit(...).
util.crud.js provides:
waw.crud.config(part, config)— registers hooks and rules for CRUD actions into thewawobject under predictable nameswaw.crud.register(crud, part, unique = true)— mounts endpoints under/api/<crudName>and wires hookswaw.crud.finalize()— scanswaw.modules[*].crudand registers CRUD endpoints for each configured resource
For each CRUD resource named <crudName>, Sem mounts routes under:
/api/<crudName>/create(POST)/api/<crudName>/get...(GET; supports named variants)/api/<crudName>/fetch...(POST; supports named variants)/api/<crudName>/update...(POST; supports named variants)/api/<crudName>/unique...(POST; supports named variants)/api/<crudName>/delete...(POST; supports named variants)
waw.crud.config(part, config) reads per-action config objects and stores behavior on waw using names like:
required_<action>_<part>[_<name>](array of required body fields)ensure_<action>_<part>[_<name>](custom ensure middleware)query_<action>_<part>[_<name>]sort_<action>_<part>[_<name>],skip_...,limit_...select_...,populate_...
At request time, Sem uses these to validate required fields, authorize access, and modify query behavior.
When registering a resource, Sem resolves a Mongoose model as:
waw.<CrudCapitalName>if already present onwaw, otherwiserequire(<moduleRoot>/schema.js)(orschema_<crudName>.jswhenunique=false)
If the required schema export is a function without a name, it is invoked as Schema(waw).
Sem loads files from all modules based on filename suffix:
*.collection.js— loaded first (intended for model/schema registration)*.api.js— loaded second (intended for mounting routes/endpoints)
Each such file is required and invoked as await require(file)(waw).
Sem exposes a module scaffolding command via server/sem/cli.js:
waw add <module>/waw a <module>— creates a module under the project modules directory using the Sem template (server/sem/module/default/scaffold.js)
Sem is defined by server/sem/module.json:
after: "core"andbefore: "*"to ensure it runs after core but before other modules by default- Installs dependencies including
express,mongoose,express-session,connect-mongo,socket.io, and others used by its utilities
MIT © Web Art Work