當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript http-errors類代碼示例

本文整理匯總了TypeScript中http-errors的典型用法代碼示例。如果您正苦於以下問題:TypeScript http-errors類的具體用法?TypeScript http-errors怎麽用?TypeScript http-errors使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了http-errors類的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: express

import * as util from 'util';

const app = express();

app.use((req, res, next) => {
    if (!req) {
        next(create('Please login to view this page.', 401));
        return;
    }
    next();
});

/* Examples taken from https://github.com/jshttp/http-errors/blob/1.6.2/test/test.js */

// create(status)
let err = create(404);
err; // $ExpectType HttpError
err.name; // $ExpectType string
err.message; // $ExpectType string
err.status; // $ExpectType number
err.statusCode; // $ExpectType number
err.expose; // $ExpectType boolean
err.headers; // $ExpectType { [key: string]: string; } | undefined

// create(status, msg)
err = create(404, 'LOL');

// create(status, props)
err = create(404, {id: 1});

// create(status, props) with status prop
開發者ID:PriceSpider-NeuIntel,項目名稱:DefinitelyTyped,代碼行數:31,代碼來源:http-errors-tests.ts

示例2: return

 return (request, __, next) => {
   if (request.method === 'GET' || request.method === 'HEAD') {
     next(createError(404));
   } else {
     next(createError(501));
   }
 };
開發者ID:edhager,項目名稱:intern,代碼行數:7,代碼來源:unhandled.ts

示例3: next

app.use((req, res, next) => {
    if (!req) {
        next(create('Please login to view this page.', 401));
        return;
    }
    next();
});
開發者ID:PriceSpider-NeuIntel,項目名稱:DefinitelyTyped,代碼行數:7,代碼來源:http-errors-tests.ts

示例4: createError

export const compareBHash = async (value: string, hash: string): Promise<true> => {
    const isValid = await bcrypt.compare(value, hash);

    if (!isValid) {
        throw createError(401, 'No match found');
    }

    return isValid;
};
開發者ID:benjambles,項目名稱:my-own-world,代碼行數:9,代碼來源:blowfish.ts

示例5: greet

router.get("/", (req, res, next) => {
  const name = req.query.name as string;

  if (name) {
    res.send({
      greetingMessage: greet(name)
    });
  } else {
    const err = HttpErrors(400, "Bad Request");
    next(err);
  }
});
開發者ID:AKIRA-MIYAKE,項目名稱:Typeyard,代碼行數:12,代碼來源:index.ts

示例6: function

registerSuite('lib/middleware/finalError', function() {
  let handler: (error: any, request: any, response: any, next: any) => void;
  let request: MockRequest;
  let response: MockResponse;
  let next: SinonSpy;

  return {
    beforeEach() {
      handler = finalError();
      request = new MockRequest('GET', '/foo/bar.js');
      response = new MockResponse();
      next = spy();
    },

    tests: {
      'exposed message'() {
        const error = createError(500, 'b0rked', { expose: true });

        handler(error, request, response, next);

        assert.isFalse(next.called);
        assert.match(response.data, /500 b0rked/);
        assert.strictEqual(response.statusCode, 500);
      },

      'hidden message'() {
        const error = createError(404, 'b0rked', { expose: false });

        handler(error, request, response, next);

        assert.isFalse(next.called);
        assert.match(response.data, /404 Not Found/);
        assert.strictEqual(response.statusCode, 404);
      }
    }
  };
});
開發者ID:edhager,項目名稱:intern,代碼行數:37,代碼來源:finalError.ts

示例7: next

        readFile(wholePath, 'utf8', (error, data) => {
          // The server was stopped in the middle of the file read
          if (context.stopped) {
            return;
          }

          if (error) {
            return next(createError(404, error, { expose: false }));
          }

          // providing `wholePath` to the instrumenter instead of a
          // partial filename is necessary because lcov.info requires
          // full path names as per the lcov spec
          data = executor.instrumentCode(data, wholePath);
          codeCache[wholePath] = {
            // strictly speaking mtime could reflect a previous
            // version, assume those race conditions are rare
            mtime,
            data
          };
          send(contentType, data);
        });
開發者ID:edhager,項目名稱:intern,代碼行數:22,代碼來源:instrument.ts

示例8: Error

app.use(function(req: Request, res: Response, next: NextFunction) {
    let err: HttpError = Error("Not Found");
    err.status = 404;
    next(err);
});
開發者ID:phroph,項目名稱:node-factorio-launcher,代碼行數:5,代碼來源:app.ts


注:本文中的http-errors類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。