本文整理匯總了TypeScript中helmet.default方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript helmet.default方法的具體用法?TypeScript helmet.default怎麽用?TypeScript helmet.default使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類helmet
的用法示例。
在下文中一共展示了helmet.default方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: express
database.connect().then((success) => {
if (success === true) {
log.info('database connected');
try {
// setup express application and middlewares
const app: express.Application = express();
container.registerInstance(express, app);
container.registerTransient(Router);
app.use(compression());
app.use(helmet());
app.use(requestHandlers.headersToLower);
app.use(requestHandlers.remoteAddressAppender);
// TODO: configure cors correctly for security concerns
app.use(cors());
// add necessary headers
app.use((req, res, next) => {
res.header('Content-*', '*');
res.header('Cache-Control', 'no-cache, no-store, must-revalidate');
res.header('Pragma', 'no-cache');
res.header('Expires', '0');
next();
});
app.use(queryHandlers.toLower);
app.use(bodyParser.json({ type: '*/json', limit: '1Mb' }));
app.set(PORT_KEY, appConfig.server.port || DEFAULT_PORT);
// fulfil pre-flight promise request
app.options('*', (req, res) => {
if (appConfig.server.allowPreFlightRequest === true) {
res.status(StatusCodes.OK).json({
success: true
});
} else {
res.status(StatusCodes.FORBIDDEN).json({
success: false
});
}
});
if (appConfig.server.enableTestEndpoint === true) {
app.get('/api/test', (req, res) => {
res.json({
message: 'hello beowulf'
});
});
}
// START SERVICES HERE
const svcManager: ServiceManager = container.get(ServiceManager);
svcManager.startServices();
app.use(express.static(path.join(__dirname, PUBLIC_DIRECTORY)));
// notify of bad request
const requestLogger = new requestHandlers.RequestLogger(log);
app.use(requestLogger.unhandled);
// log unhandled errors
const errorLogger = new errorHandlers.ErrorMiddleware(log);
app.use(errorLogger.log);
const httpServer: Server = app.listen(app.get('port'), (error: Error) => {
if (error != null) {
log.error(error);
} else {
log.info('server listening over insecure http on port ' + app.get(PORT_KEY));
// we have to start active platforms
log.info('starting active platforms');
}
});
} catch (ex) {
(log as any).fatal(ex, 'fatal error in server start up');
database.close();
process.exit(1);
}
} else {
log.error('database connection failed');
return;
}
}).catch((err) => {
示例2:
});
mongoose.connect(config.server.user.connectionString);
/**
* redis events
*/
client.on('order-closed', (order) => {
console.log('ORDER RECEIVED!!!!', JSON.parse(order));
});
/**
* Express
*/
app.use(morgan('dev'));
app.use(helmet());
app.use(json());
app.use(urlencoded({extended: false}));
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', '_id, Authorization, Origin, X-Requested-With, Content-Type, Accept');
next();
});
/**
* Add 'user' variable to request, holding userID
*/
app.use((req: any, res, next) => {
req.user = {id: req.headers['_id']};
next();
});
示例3: runServer
export function runServer(db: Knex, augur: Augur, controlEmitter: EventEmitter = new EventEmitter()): RunServerResult {
const app: express.Application = express();
app.use(helmet({
hsts: false,
}));
app.use(bodyParser.json({
reviver: addressFormatReviver,
}));
const servers: ServersData = runWebsocketServer(db, app, augur, websocketConfigs, controlEmitter);
app.get("/", (req, res) => {
res.send("Augur Node Running, use /status endpoint");
});
app.get("/status", (req, res) => {
try {
if (!isSyncFinished()) {
res.status(503).send({ status: ServerStatus.SYNCING, reason: "server syncing" });
return;
}
const networkId: string = augur.rpc.getNetworkID();
const universe: Address = augur.contracts.addresses[networkId].Universe;
getMarkets(db, augur, {universe} as t.TypeOf<typeof GetMarketsParams>).then((result: any) => {
if (result.length === 0) {
res.status(503).send({ status: ServerStatus.DOWN, reason: "No markets found", universe });
} else {
res.send({ status: ServerStatus.UP, universe });
}
}).catch((e) => { throw e; });
} catch (e) {
res.status(503).send({ status: ServerStatus.DOWN, reason: e.message });
}
});
app.get("/status/database", (req, res) => {
if (!isSyncFinished()) {
res.status(503).send({ status: ServerStatus.SYNCING, reason: "server syncing" });
return;
}
const maxPendingTransactions: number = (typeof req.query.max_pending_transactions === "undefined") ? 1 : parseInt(req.query.max_pending_transactions, 10);
if (isNaN(maxPendingTransactions)) {
res.status(422).send({ error: "Bad value for max_pending_transactions, must be an integer in base 10" });
} else {
const waitingClientsCount = db.client.pool.pendingAcquires.length;
const status = (maxPendingTransactions > waitingClientsCount) ? ServerStatus.UP : ServerStatus.DOWN;
res.status(status === ServerStatus.UP ? 200 : 503).send({
status,
maxPendingTransactions,
pendingTransactions: waitingClientsCount,
});
}
});
app.get("/status/blockage", (req, res) => {
if (!isSyncFinished()) {
res.status(503).send({ status: ServerStatus.SYNCING, reason: "server syncing" });
return;
}
db("blocks").orderBy("blockNumber", "DESC").first().asCallback((err: Error, newestBlock: any) => {
if (err) return res.status(503).send({ error: err.message });
if (newestBlock == null) return res.status(503).send({ error: "No blocks available" });
const timestampDelta: number = Math.round((Date.now() / 1000) - newestBlock.timestamp);
const timestampDeltaThreshold = (typeof req.query.time === "undefined") ? 120 : parseInt(req.query.time, 10);
if (isNaN(timestampDeltaThreshold)) {
res.status(422).send({ error: "Bad value for time parameter, must be an integer in base 10" });
}
const status = timestampDelta > timestampDeltaThreshold ? ServerStatus.DOWN : ServerStatus.UP;
return res.status(status === ServerStatus.UP ? 200 : 503).send(Object.assign({ status, timestampDelta }, newestBlock));
});
});
app.get("/status/sync", (req, res) => {
if (!isSyncFinished()) {
res.status(503).send({ status: ServerStatus.DOWN, reason: "server syncing" });
} else {
res.send({ status: ServerStatus.UP, reason: "Finished with sync" });
}
});
app.use(cors());
app.post("*", cors(), async (req, res) => {
try {
const result = await dispatchJsonRpcRequest(db, req.body as JsonRpcRequest, augur);
res.send(makeJsonRpcResponse(req.body.id, result || null));
} catch (err) {
res.status(500);
res.send(makeJsonRpcError(req.body.id, JsonRpcErrorCode.InvalidParams, err.message, false));
}
});
return { app, servers };
}