Default scaffold for restify server in node. This is a work in progress.
Begin with npm install
.
This project uses TypeScript with ts-node and nodemon for development.
Execute npm start
to run the server on the localhost with hot reload.
Execute npm test
to execute unit tests (mocha).
Execute npm run build
to create a production bundle ready to be deployed and stripped of any non-production artifacts. This process uses the localbuilder.ts and localbuilder.config.json scripts
to deploy. Final bundle does not include nodemon or TypeScript.
- Create a local copy of
.env
as a copy ofcfg.env
. - Create any environment variables desired inside
.env
with format:SOMEUID=JOE
SOMEPWD=SMITH
- Confirm entries in
config/default.json
andconfig/production.json
- API Prefix path (default to
/api/
) - Serve Static Content (any non-empty string maps to root of project. Use absolute or relative paths. Defaults to
./public/
) - API Port (defaults to 8080)
- API Prefix path (default to
- Create services in new folder (ex.
/app/shoes
). Can mimic pattern within/common/healthCheck.ts
. - Create route handler inside
/routes/router.ts
to map to new service above. - If services rely on an external database connection, be sure to wrap the
rs.start()
method call ofindex.ts
inside the callback from that connections startup asynchronous method e.g. don't start the server until the database connection pool has been started up and any necessary seeding processes completed.- e.g.
database.connection.open((err) => if (!err) rs.start() )
<- where the parameter to the open method is an asynchronous callback called only after the connection is established.
- e.g.
- Create Unit Tests in
./test/
folder for each new service method created.
Running npm start
uses hot reload meaning any changes to *.ts
files (or others) causes nodemon
to automatically restart the server with the changes.
Test any new services using your favorite REST tool like Rested, Postman, any modern browser etc.
Run any unit tests at any time in a new console using npm test
.
You can place any static content into (default ./public/
) folder and those assets will be served statically over the API Port. The path used is mapped inside /config/default.json
. Setting the variable serveStaticPath
to an empty string or removing it from the configuration file results in no static content being served.
- The endpoint from the client maps to the root of the webserver e.g.
serveStaticPath="./public/"
will map any request tolocalhost:8080/somefile.txt
toprocess.cwd()/public/somefile.txt
- Another example request
localhost:8080/shoes/vendors.json
would map toprocess.cwd()/public/shoes/vendor.json
Anything created in ./public/
will be served as is e.g. Angular, React, Vue, PWA
To create a new Web API:
- Create Service file in
./app/
folder - Export Service Class from
./app/index.ts
- Create Route in
./routes/
- Export Route in
./routes/index.ts
- Import Route in
./routes/router.ts
- Create Route Handler inside
Router.init()
method - All handlers should be asynchronous
- Write Unit Tests in
./test
folder for service - Document API in
swagger.json
document
- Support for Swagger UI rendering off of
./swagger.io