{"id":23391322,"url":"https://github.com/maples7/express-final-response","last_synced_at":"2026-04-16T14:08:52.265Z","repository":{"id":48003355,"uuid":"82271727","full_name":"Maples7/express-final-response","owner":"Maples7","description":"The very last middleware of Express to handle every response of every request.","archived":false,"fork":false,"pushed_at":"2022-12-06T22:17:47.000Z","size":681,"stargazers_count":1,"open_issues_count":6,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-07T02:04:21.751Z","etag":null,"topics":["express","express-js","express-middleware","nodejs","router","routes"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Maples7.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-17T07:52:43.000Z","updated_at":"2021-03-31T04:12:04.000Z","dependencies_parsed_at":"2023-01-24T11:45:17.090Z","dependency_job_id":null,"html_url":"https://github.com/Maples7/express-final-response","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Maples7%2Fexpress-final-response","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Maples7%2Fexpress-final-response/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Maples7%2Fexpress-final-response/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Maples7%2Fexpress-final-response/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Maples7","download_url":"https://codeload.github.com/Maples7/express-final-response/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247865839,"owners_count":21009235,"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":["express","express-js","express-middleware","nodejs","router","routes"],"created_at":"2024-12-22T04:14:51.398Z","updated_at":"2026-04-16T14:08:52.197Z","avatar_url":"https://github.com/Maples7.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# express-final-response\n[![Build Status](https://travis-ci.org/Maples7/express-final-response.svg?branch=master)](https://travis-ci.org/Maples7/express-final-response)\n[![Coverage Status](https://coveralls.io/repos/github/Maples7/express-final-response/badge.svg?branch=master)](https://coveralls.io/github/Maples7/express-final-response?branch=master)\n[![npm version](https://badge.fury.io/js/express-final-response.svg)](https://badge.fury.io/js/express-final-response)           \n[![NPM](https://nodei.co/npm/express-final-response.png?downloads=true\u0026downloadRank=true\u0026stars=true)](https://nodei.co/npm/express-final-response/)\n[![NPM](https://nodei.co/npm-dl/express-final-response.png?months=6\u0026height=3)](https://nodei.co/npm/express-final-response/)       \nThe very last middleware of Express to handle every response of every request.\n\n## Features\n- Both json-type response and HTML-type response (with some Express template engine) are supported at the same time\n- Log every request while neccessary, and the logger is decided by you\n- Every response has consistent and standard format, and also you are highly encouraged to customize yours for every distinct response\n- Well tested\n\n## Usage\n### Installation\n`yarn add express-final-response` or `npm install express-final-response --save`;\n\n### Work with Express\nLet me just give you a clear example:\n```js\nconst path = require('path');\nconst express = require('express');\nconst routes = require('express-mount-routes');\nconst finalResp = require('express-final-response');\n\nconst app = express();\n\n// set Express template engine if you need to\napp.set('view engine', 'pug');\n\n// mount routes, see https://github.com/Maples7/express-mount-routes\nroutes(app, path.join(__dirname, 'controllers'));\n\n// a very easy-to-write 404 middleware thank to this awesome package\napp.use((isAPI = true) =\u003e {\n  if (isAPI) {\n    return (req, res, next) =\u003e next({status: 'APINotFound'});\n  } else {\n    // of course you can give a view to it and then it will return an HTML page to you\n    return (req, res, next) =\u003e next({status: 'PageNotFound', view: '404'});\n  }\n});\n\n// aha, finally we got the protagonist\napp.use(finalResp({isDebug: process.env.NODE_ENV !== 'production'}));\n\napp.listen(3000, function () {\n  console.log('Example app listening on port 3000!');\n});\n```\n\n### API\n```js\n// right value of `=` is default value and all parameters are optional\napp.use(finalResp({\n  // you are highly encouraged to customize yours for every distinct response, see ./lib/status.json to get the essentials: \n    // statusCode -\u003e HTTP Response Code\n    // successful -\u003e Whether a successful response or not\n    // code -\u003e Custom response code. It can be used by mobile client or front-ends to customize their own response to user. Also you'd better well classify them according to the type of response such as user module of your system or article module and make every one unique.\n    // status -\u003e this string is used to locate status in this package (it's better than using `code` because it's semantic), so make sure they are unique upon most occasions. That's to say, the former status would be replaced by the latter one who keeps the same `status` string param.\n    // desc -\u003e brief description\n  customStatuses = [],\n\n  // response encoding\n  encoding = 'utf-8',\n\n  // logger\n  logger = console,\n\n  // if response is a JSON, you can return a true JSON or a String (with `JSON.stringify(JSONResult)`)\n    // JSON -\u003e Return a true JSON\n    // String -\u003e Return a String with `JSON.stringify(JSONResult)`. This is prepared for some wired clients.\n  JSONformat = 'JSON',\n\n  // If this switch is true, all error infomation (error stack included) would be return to client while error occurs; If not, user would noly get error.message. This is prevent key infomation leak from hackers. And don't worry, all infomation will be logged.\n  isDebug = false,\n\n  // Whether only log error response or not\n  onlyLogError = false,\n\n  // if there is a global error view such as 500.pug, error response would be an HTML page with the defined error view. Also, you can customize it in each response with param `view`. \n  errorView = undefined\n}));\n```\n\n### Response in each request\nOnce you want to return result in each request, call [Express `next()` function](http://expressjs.com/en/guide/error-handling.html) (Actually, we are making use of error handling mechanism of Express) with proper params.\n\n#### If response is an error, thare are several ways to do this:\n```js\n// pass an Error and `error` status would be applied automatically.\nnext(new Error('I am an Error')); \n\n// pass an object but `msg` is an error, then `DBError` status would be applied and returned `msg` would be 'TrueError'. This way is better than former one because it can pass more infomation.\nnext({status: 'DBError', msg: new Error('TrueError')});\n\n// the view would be applied so an HTML page would be returned. If there is no view param, a JSON would be returned like above, and the infomation in the returned JSON could be used while rendering view template.\nnext({status: 'error', msg: new Error('I am an error'), view: '500'});\n``` \nResponse object would contain `statusCode`, `successful`, `code`, `status`, `desc`, `msg`, `ext`(when `isDebug` switch is on), for example:\n```js\n{ \n  statusCode: 500,\n  successful: false,\n  code: 2,\n  status: 'error',\n  desc: 'Request Error!',\n  msg: 'I am an error',\n  ext: '...'  // the whole Error object\n}\n```\n\n#### If response is normal data, do it like this:\n```js\n// the most ordinary way, `ext` is optional. `msg` here is designed for holding real data you want to return to clients or front-ends.\nnext({status: 'success', msg: 'test 1 success'});\n\n// the view would be applied so an HTML page would be returned. If there is no view param, a JSON would be returned like above, and the infomation in the returned JSON could be used while rendering view template.\nnext({status: 'success', msg: 'test 1 success', view: '200'});\n```\nResponse object would contain `statusCode`, `successful`, `code`, `status`, `desc`, `msg`(optional), `ext`(optional), for example:\n```js\n{\n  statusCode: 200,\n  successful: true,\n  code: 0,\n  status: 'success',\n  desc: 'Request Successful!',\n  msg: 'test 1 success' \n}\n```\n\n#### See default statuses definition\nThere are so many other defaut statuses you can choose besides `success` and `error`, make sure to see [./lib/status.json](lib/status.json) to catch them. Of course, you can define your own statuses for the benefit of `customStatus` parameter like what I have told you.\n \n\nYou are welcomed to review _test.js_, _controllers_ dir and _views_ dir in this project for more information of usage.\n\n## LICENSE\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaples7%2Fexpress-final-response","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaples7%2Fexpress-final-response","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaples7%2Fexpress-final-response/lists"}