{"id":26355888,"url":"https://github.com/kshirish/node-boilerplate","last_synced_at":"2025-03-16T13:18:23.000Z","repository":{"id":35727244,"uuid":"40005694","full_name":"kshirish/node-boilerplate","owner":"kshirish","description":null,"archived":false,"fork":false,"pushed_at":"2015-08-12T11:41:04.000Z","size":156,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2024-03-15T22:03:39.115Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kshirish.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-07-31T13:05:49.000Z","updated_at":"2024-03-15T22:03:39.116Z","dependencies_parsed_at":"2022-07-20T23:33:25.751Z","dependency_job_id":null,"html_url":"https://github.com/kshirish/node-boilerplate","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kshirish%2Fnode-boilerplate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kshirish%2Fnode-boilerplate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kshirish%2Fnode-boilerplate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kshirish%2Fnode-boilerplate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kshirish","download_url":"https://codeload.github.com/kshirish/node-boilerplate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243871892,"owners_count":20361380,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-16T13:18:22.273Z","updated_at":"2025-03-16T13:18:22.994Z","avatar_url":"https://github.com/kshirish.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Node-boilerplate\n\n## Things you didn't know\n\n- Setting up more than one environment has several perks.\n```\n$ NODE_ENV = Production node app.js\n```\n\n- `app.locals` is a place to store the data which persits throughtout the lifetime of an app.\n\n- `res.locals` is a place to store the data which lasts only till the request.\n\n- However you cannot access local variables in middleware though you can  access local variables in templates rendered within the application. This is useful for providing helper functions to templates, as well as app-level data.\n\n- Subapp is an additional instance of express created to handle the routing part which is often the most complicated one. \n\n```js\n\nvar admin = express();\n\nadmin.get('/', function (req, res) {\n  console.log(admin.mountpath); // [ '/adm*n', '/manager' ]\n  res.send('Admin Homepage');\n})\n\nvar secret = express();\n\nsecret.get('/', function (req, res) {\n  console.log(secret.mountpath); // /secr*t\n  res.send('Admin Secret');\n});\n\n// mount the 'secret' subapp on '/secr*t', on the 'admin' sub app\nadmin.use('/secr*t', secret);\n\n// mount the 'admin' subapp on '/adm*n' and '/manager', on the parent app\napp.use(['/adm*n', '/manager'], admin); \n\n```\n- Authenticate api calls \n```js\napp.all('/api/*', requireAuthentication);\n```\n\n- Disable/Enable\n```js\napp.disable('foobar') === app.set('foobar', false);\napp.disabled('foobar');  // true\napp.enable('foobar') === app.set('foobar', true);\napp.enabled('foobar');  // true\n```\n\n- Middleware will be executed for every request to the app\n```js\napp.use(function (req, res, next) {\n  /* do something here */\n  next();\n})\n```\n\n- Series of middlewares\n```js\nfunction mw1(req, res, next) { console.log('mw1'); next(); }\nfunction mw2(req, res, next) { console.log('mw2'); next(); }\n\nvar r1 = express.Router();\nr1.get('/', function (req, res, next) { console.log('r1'); next(); });\n\nvar r2 = express.Router();\nr2.get('/', function (req, res, next) { console.log('r2'); next(); });\n\nvar subApp = express();\nsubApp.get('/', function (req, res, next) { console.log('subapp'); next(); });\n\n// executes the callbacks in the provided order\napp.use('/someRoute', mw1, [mw2, r1, r2], subApp);\n```\n\n- An error-handling middleware is same as other middlewares, except with four arguments instead of three.\n```js\napp.use(function(err, req, res, next) {\n  console.error(err.stack);\n  res.status(500).send('Something broke!');\n});\n```\n\n- Express uses the `debug` module internally to log information about route matches, middleware in use, application mode, and the flow of the request-response cycle.\n```js\n$ DEBUG=express:* node index.js\n```\n\n- In HTTP, the `Content-Disposition`: attachment response header is usually used to hint to the client to present the response body as a downloadable file. Typically, when receiving such a response, a Web browser will prompt the user to save its content as a file instead of displaying it as a page in a browser window, with the filename parameter suggesting the default file name (this is useful for dynamically generated content, where deriving the filename from the URL may be meaningless or confusing to the user).\n```js\n// now this is how we do it in Express\n\nres.attachment();\n// Content-Disposition: attachment\n\nres.attachment('path/to/logo.png');\n// Content-Disposition: attachment; filename=\"logo.png\"\n// Content-Type: image/png\n\n```\n\n- Now, the above `res.attachment` basically just sets the `header`, you have to `res.send` the file as well. But you're in luck, express got a helper for you. \n```js\nres.download('/akugvks-ygksyug-reyyrt7tr74.pdf', 'payslip.pdf');\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkshirish%2Fnode-boilerplate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkshirish%2Fnode-boilerplate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkshirish%2Fnode-boilerplate/lists"}