Quick start application setup with sensible defaults baked in.
Demo: https://cds-node-starter.herokuapp.com
Should you use this? It depends ;)
Don't like the way it's setup -> it's an Express server so do your thing app.js
- Create an
empty
Github repo (must be empty)
git remote add upstream git@github.com:cds-snc/node-starter-app.git
git pull upstream master
git remote -v // ensure the remotes are setup properly
// you should see
origin git@github.com:cds-snc/your-repo.git (fetch)
origin git@github.com:cds-snc/your-repo.git (push)
upstream git@github.com:cds-snc/node-starter-app.git (fetch)
upstream git@github.com:cds-snc/node-starter-app.git (push)
npm install
npm run dev
- Create a new route directory
➡️ or copy past and rename the routes/_sample_route
- Add your route files
- controller
- view file
- optionally schema.js (if this is a form view)
step5.controller.js //express route handler
step5.pug //the view file
schema.js //if this is a form
- Register the route via routes.config.js
// config/routes.config.js
...
const routes = [
{ name: "step5", path: "/step5" },
];
...
//Add the path to your route code in configRoutes
const configRoutes = app => {
...
require("../routes/step5/step5.controller")(app);
}
Note: Delete unused route(s) directories as needed.
Saved data is available via getSessionData(req)
app.get(route.path, (req, res) => {
res.render(name, {
data: getSessionData(req),
name
});
});
+textInput('form.passport_expiry', null, 'form.passport_expiry.desc')(class='w-3-4', id='expiry' name='expiry', autofocus, value=data.expiry)
input(name='name', type='hidden', value=name)
input(name='nonce', type='hidden', value=nonce)
Redirects are handled via doRedirect based on a name
value (the name of the current route) sent via in the req.body. The doRedirect function will do a look up for the next route based on the routes config.
// step5.controller post route
app.post(
route.path,
checkNonce,
checkSchema(Schema),
checkErrors(name),
doRedirect
);
For cases where the redirect is not straight forward you can handle manually.
(req, res, next) => {
const confirm = req.body.confirm;
if (confirm === "Yes") {
const nextRoute = getNextRoute(name);
return res.redirect(nextRoute.path);
}
res.send("you said no");
}
Text on pages is supplied via ids
block variables
-var title = __('personal.title')
block content
h1 #{title}
div
p #{__('personal.intro')}
form.cra-form(method='post')
// locales/en.json
"personal.title": "Personal Information",
"personal.intro": "Intro copy goes here",
"form.fullname": "Full name",
- Form validation is built into the form schema files and use validator.js to validate input
- Templates currenty use Pug (formerly Jade). You can use whatever you like for a template-engine. There's even a server rendered React engine. That said, it's bring your own layouts and helper files.
- Accessible out of the box
- Keep code routes / view(s) / schemas as portable (self-contained) as possible.
- If code i.e custom validators from the routes can be re-used it should be pulled up to the
app
level - App level code (app.js) should be touched a little as possible when building a new app based on the starter
- Implement best practices from Form design: from zero to hero all in one blog post
Routes should act like a plugin. i.e. Project B has a page you need, copy the route directory and add that route to your config.
- This project aims to allow you to hit the ground running. It's not meant to be a be all end all defacto solution.
- Adding tests for sample routes
- Adding more tests for utility functions
This project is based on the orginal code https://github.com/cds-snc/cra-claim-tax-benefits it was born out of wanting to use that code as a base without the need to remove the unused parts everytime a new project is started.
See:
- https://github.com/cds-snc/notification-demo-service/commit/ab24e79268626e1431b301fb91614b40f9615086
- https://github.com/cds-snc/2620-passport-renewal/commit/eb41bf83825b9d8c4a56427e0cd199ccc23089eb
Starter Cloud Build / Cloud Run setup is in place if you prefer to deploy via GCP see
notification-demo-service
which is setup to deploy using a tag.