{"id":664,"date":"2019-01-07T10:14:48","date_gmt":"2019-01-07T02:14:48","guid":{"rendered":"https:\/\/wyxxt.org.cn\/?p=664"},"modified":"2023-12-04T17:04:42","modified_gmt":"2023-12-04T09:04:42","slug":"nodeexpress%e6%a1%86%e6%9e%b6%e9%94%99%e8%af%af%e5%a4%84%e7%90%86%e6%9c%ba%e5%88%b6","status":"publish","type":"post","link":"https:\/\/wyxxt.org.cn\/?p=664","title":{"rendered":"node+express\u6846\u67b6\u9519\u8bef\u5904\u7406\u673a\u5236"},"content":{"rendered":"<h3>APIError.js<\/h3>\n<pre><code class=\"language-javascript line-numbers\">const httpStatus = require('http-status');\n\n\/**\n* @extends Error\n*\/\nclass ExtendableError extends Error {\n    constructor(message, status, isPublic) {\n        super(message);\n        this.name = this.constructor.name;\n        this.message = message;\n        this.status = status;\n        this.isPublic = isPublic;\n        this.isOperational = true; \/\/ This is required since bluebird 4 doesn't append it anymore.\n        Error.captureStackTrace(this, this.constructor.name);\n    }\n}\n\n\/**\n* Class representing an API error.\n* @extends ExtendableError\n*\/\nclass APIError extends ExtendableError {\n    \/**\n    * Creates an API error.\n    * @param {string} message - Error message.\n    * @param {number} status - HTTP status code of error.\n    * @param {boolean} isPublic - Whether the message should be visible to user or not.\n    *\/\n    constructor(message, status = httpStatus.INTERNAL_SERVER_ERROR, isPublic = false) {\n        super(message, status, isPublic);\n    }\n}\n\nmodule.exports = APIError;\n<\/code><\/pre>\n<h3>express.js<\/h3>\n<pre><code class=\"language-javascript line-numbers\">const httpStatus = require('http-status');\n\n\n\/\/ if error is not an instanceOf APIError, convert it.\n\napp.use((err, req, res, next) =&gt; {\n\n    if (err instanceof expressValidation.ValidationError) {\n\n        \/\/ validation error contains errors which is an array of error each containing message[]\n\n        const unifiedErrorMessage = err.errors.map(error =&gt; error.messages.join('. ')).join(' and ');\n\n        const error = new APIError(unifiedErrorMessage, err.status, true);\n\n        return next(error);\n\n    } else if (!(err instanceof APIError)) {\n\n        const apiError = new APIError(err.message, err.status, err.isPublic);\n\n        return next(apiError);\n\n    }\n\n    return next(err);\n\n});\n\n\n\n\/\/ catch 404 and forward to error handler\n\napp.use((req, res, next) =&gt; {\n\n    const err = new APIError('API not found', httpStatus.NOT_FOUND);\n\n    return next(err);\n\n});\n\n\/\/ error handler, send stacktrace only during development\n\napp.use((err, req, res, next) =&gt; \/\/ eslint-disable-line no-unused-vars\n\n    res.status(err.status).json({\n\n        status: 0,\n\n        errmsg: err.isPublic ? err.message : httpStatus[err.status],\n\n        stack: config.env === 'development' ? err.stack : {}\n\n    })\n\n);\n<\/code><\/pre>\n<h4>Model\u5c42\u3001Service\u5c42\u76f4\u63a5throw APIError<\/h4>\n<p><em>model\u4e4b\u95f4\u4ea4\u53c9\u8c03\u7528\u65f6\uff0c\u63a7\u5236\u5668\u65b9\u6cd5\u5185\u8c03\u7528\u591a\u4e2amodel\u65f6\uff0c\u9700\u8981\u4f7f\u7528service<\/em><\/p>\n<h4>Controller\u5c42\u76f4\u63a5next(err)<\/h4>\n<h4>\u4f7f\u7528\u6b64\u65b9\u6cd5\u53ef\u4ee5\u4f7fexpress\u5b9e\u73b0\u5206\u5c42<\/h4>\n","protected":false},"excerpt":{"rendered":"<p>APIError.js const httpStatus = require(&#8216;http-status&#8217;);  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[15],"tags":[414],"class_list":["post-664","post","type-post","status-publish","format-standard","hentry","category-15","tag-node"],"_links":{"self":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/664","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=664"}],"version-history":[{"count":1,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/664\/revisions"}],"predecessor-version":[{"id":665,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/664\/revisions\/665"}],"wp:attachment":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}