My nodejs + express security and performance playground (boilerplate).
- Cookie auth (secure, http-only, sameSite)
- Signed session + sliding expiration
- Cross-Site Request Forgery (CSRF)
- Cross-Site WebSocket Hijacking (CSWSH)
- Content-Security-Policy (CSP, nonce)
- Strict-Transport-Security (HSTS)
- Public-Key-Pins (HPKP)
- X-Frame-Options
- X-XSS-Protection
- X-Powered-By
- X-Download-Options
- X-Content-Type-Options
- Rate limits (number of requests per second)
- HTTP2
- Client caching (cache-control, e-tags, expires, last-modified, cache busting)
- Client assets minification
- GZIP, deflate
- Precompressed assets (Brotli and Gzip support)
- Imagemin
- Node cluster
- Memory and redis caching
- HTTPS
- Redis store (session, caching)
- Web sockets (socket.io, cookies authentication, shared session, redis store)
- Logging (winston -> Console, FileSystem)
- Custom errors (4XX, 5XX)
- Configuration + Environment (config.json + config.dev.json)
- Nodemon
- Webpack + babel + SASS + postcss + autoprefixer
- ES6 modules (babel, babel-preset-env)
- Unit tests (ES6, mocha + chai)
- E2E tests (ES6, mocha + chai, supertest + superagent)
- ESLint 3 (linting server, client, tests and pug templates)
- npm scripts (npm-run-all, cross-env, client tasks, server tasks)
- http://realfavicongenerator.net/
- Pug view engine
Disclaimer: The main purpose of this repository is to demonstrate security, caching and performance principles, it's not about DB, UI, UX.
Certificate
The application runs only on HTTPS, so a certificate is required. If you want to just try it locally, then generate self-signed certificate online for domain localhost
and save both files into the server/certificates/
folder. Name them server.cert
and server.key
.
Redis The application uses Redis to store sessions, caching, websockets and saving rate limits. Please follow their installation guide.
- run
yarn install
ornpm install
- run
yarn run start
ornpm run start
- open URL
https://localhost:8443/
in a browser (you can change the port if you re-run the previous command withPORT=8444 yarn run start
) - use any username and password to log in. There is no real DB behind the app, it's not the main purpose of the demo.
- run
yarn install
ornpm install
- run
yarn run build:client
- run
yarn run watch:server
(runs nodemon so any change in server's code immediately restarts server) - open URL
https://localhost:5000/
in a browser (you can change the port innodemon.json
)
The application can run as a cluster of node applications. Use "useCluster"
option in server/config/config.json
:
- "auto": no cluster in dev mode, all possible cores in prod mode.
- true: all possible cores in all modes.
- any number: spawn a given number of instances.
- anything else: no cluster in any mode.
You can change server/config/config.json
file directly. If you are in development mode and you don't want to commit your config changes, then create config.dev.json
next to the original JSON file and store your overrides there. Both configs are deeply merged together, config.dev.json
takes precedence.
- run
yarn run test:unit
to run unit tests only - run
yarn run test:vulnerabilities
to check app dependencies for vulnerabilities (using nsp)
to run e2e tests, you have to start the server in development first
- then run
yarn run test:e2e
If you want to run all tests, start the server in development mode and then run yarn run test
The application uses rate limiters to prevent hundreds of requests from the same IP address. This protection must be turned off while running e2e tests, otherwise the tests will start to fail randomly after the limits are reached. To turn it off, create config.dev.json
and add into it:
{
"rateLimiter": {
"enabled": false
}
}