本文整理匯總了TypeScript中restify.CORS函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript CORS函數的具體用法?TypeScript CORS怎麽用?TypeScript CORS使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了CORS函數的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: ShadowsocksManagerService
stats).then((managedAccessKeyRepository) => {
const managerService = new ShadowsocksManagerService(managedAccessKeyRepository);
const certificateFilename = process.env.SB_CERTIFICATE_FILE;
const privateKeyFilename = process.env.SB_PRIVATE_KEY_FILE;
// TODO(bemasc): Remove casts once https://github.com/DefinitelyTyped/DefinitelyTyped/pull/15229 lands
const apiServer = restify.createServer({
certificate: fs.readFileSync(certificateFilename),
key: fs.readFileSync(privateKeyFilename)
});
// Pre-routing handlers
apiServer.pre(restify.CORS());
// All routes handlers
const apiPrefix = process.env.SB_API_PREFIX ? `/${process.env.SB_API_PREFIX}` : '';
apiServer.pre(restify.pre.sanitizePath());
apiServer.use(restify.jsonp());
apiServer.use(restify.bodyParser());
setApiHandlers(apiServer, apiPrefix, managerService, stats, serverConfig);
// TODO(fortuna): Bind to localhost or unix socket to avoid external access.
apiServer.listen(portNumber, () => {
logging.info(`Manager listening at ${apiServer.url}${apiPrefix}`);
});
});
示例2: bootstrap
public bootstrap(port: number) {
this.restifyApplication.use(restify.CORS());
this.restifyApplication.use(restify.pre.sanitizePath());
this.restifyApplication.use(restify.acceptParser(this.restifyApplication.acceptable));
this.restifyApplication.use(restify.bodyParser());
this.restifyApplication.use(restify.queryParser());
this.restifyApplication.use(restify.authorizationParser());
this.restifyApplication.use(restify.fullResponse());
// configure API and error routes
this.restifyContactRouter.configApiRoutes(this.restifyApplication);
this.restifyContactRouter.configErrorRoutes(this.restifyApplication);
// listen on provided ports
this.restifyApplication.listen(port, () => {
console.log("%s listening at %s", this.restifyApplication.name, this.restifyApplication.url);
});
}
示例3: enableCORSFix
/**
* a crude CORS based on `Qwerios`' comment in
* https://github.com/mcavage/node-restify/issues/664
*
* See issues:
* https://github.com/mcavage/node-restify/issues/284 (closed)
* https://github.com/mcavage/node-restify/issues/664 (unresolved)
*
* @param additionalHeaders string[]
* @return void
*/
enableCORSFix(additionalHeaders: string[] = []): void {
/*************************************
Cors
**************************************/
this.server.use(restify.CORS());
let headers = [
"authorization",
"withcredentials",
"x-requested-with",
"x-forwarded-for",
"x-real-ip",
"user-agent",
"keep-alive",
"host",
"accept",
"connection",
"upgrade",
"content-type",
"dnt",
"if-modified-since",
"cache-control"
];
restify.CORS.ALLOW_HEADERS = [
...restify.CORS.ALLOW_HEADERS || [],
...headers,
...additionalHeaders || []
];
// Manually implement the method not allowed handler to fix failing
// preflights
this.server.on(
"MethodNotAllowed",
(request: Request, response: Response) => {
if (request.method.toUpperCase() !== "OPTIONS") {
response.send(new restify.MethodNotAllowedError());
return;
} else {
// Send the CORS headers
//
response.header("Access-Control-Allow-Credentials", true);
response.header(
"Access-Control-Allow-Headers",
restify.CORS.ALLOW_HEADERS.join(", ")
);
response.header(
"Access-Control-Allow-Methods",
"GET, HEAD, POST, PUT, DELETE, OPTIONS"
);
response.header(
"Access-Control-Allow-Origin",
request.headers.origin
);
response.header("Access-Control-Max-Age", 0);
response.header("Content-type", "text/plain charset=UTF-8");
response.header("Content-length", 0);
response.send(204);
}
}
);
}
示例4: require
import * as restify from 'restify';
import * as fs from 'fs';
import {settings} from './config';
import {logger} from './logger';
import * as auth from './core/auth';
var server = restify.createServer({
name: settings.name,
log: logger
});
restify.CORS.ALLOW_HEADERS.push('authorization');
server.use(restify.CORS());
server.pre(restify.pre.sanitizePath());
server.use(restify.acceptParser(server.acceptable));
server.use(restify.bodyParser());
server.use(restify.queryParser());
server.use(restify.authorizationParser());
server.use(restify.fullResponse());
server.use(auth.Auth());
fs.readdirSync(__dirname + '/routes').forEach(function (routeConfig:string) {
if (routeConfig.substr(-3) === '.js') {
var route = require(__dirname + '/routes/' + routeConfig);
route.routes(server);
}
});
server.listen(settings.port, function () {
console.log(`INFO: ${settings.name} is running at ${server.url}`);
});
示例5:
server.address().family;
server.address().address;
server.listen("somePath", send);
server.close();
server.use(restify.acceptParser(server.acceptable));
server.use(restify.authorizationParser());
server.use(restify.dateParser());
server.use(restify.queryParser());
server.use(restify.jsonp());
server.use(restify.gzipResponse());
server.use(restify.bodyParser());
server.use(restify.CORS({
origins: ['https://foo.com', 'http://bar.com', 'http://baz.com:8081'],
credentials: true,
headers: ['x-foo']
}));
server.use(restify.throttle({
burst: 100,
rate: 50,
ip: true,
overrides: {
'192.168.1.1': {
rate: 0,
burst: 0
}
}
}));
server.on('after', restify.auditLogger({
示例6: BookStoreController
import { RedirectController } from './src/redirect-controller';
let port = 3000;
let bsController = new BookStoreController(new BookStore());
let serverController = new ServerController();
let redirectController = new RedirectController();
var server = createServer({
formatters: {
'application/json': function (req, res, body, cb) {
return cb(null, saveStringify(body));
}
}
});
server.use(bodyParser());
server.use(CORS({}));
server.use(queryParser());
server.get('/swagger.json', serverController.getFixedSwaggerJson.bind(serverController));
// serve redirects
server.get(/^\/(a\/|avatar\/)/, redirectController.avatarRedirect.bind(redirectController));
server.get(/^\/(app|bm|it|ngh|start|two|cover|quotes|b\/|x\/)/, redirectController.redirect.bind(redirectController));
// serve public folder
server.get(/^(?!\/(book|info)).*/, serveStatic({
directory: __dirname + '/public/',
default: 'index.html'
}));