本文整理汇总了TypeScript中http-errors.default方法的典型用法代码示例。如果您正苦于以下问题:TypeScript http-errors.default方法的具体用法?TypeScript http-errors.default怎么用?TypeScript http-errors.default使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类http-errors
的用法示例。
在下文中一共展示了http-errors.default方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: return
return (request, __, next) => {
if (request.method === 'GET' || request.method === 'HEAD') {
next(createError(404));
} else {
next(createError(501));
}
};
示例2: next
app.use((req, res, next) => {
if (!req) {
next(create('Please login to view this page.', 401));
return;
}
next();
});
示例3: 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;
};
示例4: 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);
}
});
示例5: 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);
}
}
};
});
示例6: 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);
});
示例7: 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
示例8: Error
app.use(function(req: Request, res: Response, next: NextFunction) {
let err: HttpError = Error("Not Found");
err.status = 404;
next(err);
});