本文整理汇总了TypeScript中http.ServerResponse.setHeader方法的典型用法代码示例。如果您正苦于以下问题:TypeScript ServerResponse.setHeader方法的具体用法?TypeScript ServerResponse.setHeader怎么用?TypeScript ServerResponse.setHeader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类http.ServerResponse
的用法示例。
在下文中一共展示了ServerResponse.setHeader方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: setTimeout
echoServer.use(function(req: IncomingMessage, res: ServerResponse) {
let urlParts = req.url.split("?");
let returnResponse = () => {
res.setHeader('Content-Type', 'text/plain');
res.writeHead(200);
req.pipe(res);
};
if (urlParts.length > 1) {
const parameters = querystring.parse(urlParts[1]);
if (parameters.d) {
res.setHeader('d', parameters.d);
}
if (parameters.t) {
res.setHeader('t', parameters.t);
setTimeout(returnResponse, parameters.t)
}
else {
returnResponse();
}
return;
}
returnResponse();
});
示例2: nocache
return function nocache (_req: IncomingMessage, res: ServerResponse, next: () => void) {
res.setHeader('Surrogate-Control', 'no-store');
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
next();
};
示例3: dispatch
static dispatch(request : IncomingMessage, response : ServerResponse)
{
response.statusCode = 200;
response.setHeader('Content-Type', 'application/json');
var headers = request.headers;
var method : RequestMethod = toRequestMethod(request.method);
var url : RequestUrl = new RequestUrl(request.url);
var body = [];
request.on('error', function (err) {
console.error(err);
}).on('data', function(bodyChunk : string) {
body.push(bodyChunk);
}).on('end', function (){
response.on('error', function(err) {
console.error(err);
});
var dispatcher : IDispatcher = DispatchGenerator.generate(headers, method, url);
var responseBody : Object = dispatcher.dispatch(method);
response.write(JSON.stringify(responseBody));
response.end();
});
}
示例4: function
return function (req: ServerRequest, resp: ServerResponse) {
resp.setHeader('Content-Type', 'application/json')
if (req.method === 'GET') {
bot.validate(req, resp)
} else if (req.method === 'POST') {
bot.parseMessagingEvent(req, resp)
} else {
resp.statusCode = 400
resp.end()
}
}
示例5:
const callback = (error, result) => {
if (error) throw error;
res.statusCode = result.statusCode;
for (let key in result.headers) {
if (result.headers.hasOwnProperty(key)) {
res.setHeader(key, result.headers[key]);
}
}
res.write(result.body);
res.end();
};
示例6: checkForKillCommand
export function checkForKillCommand(req: IncomingMessage, res: ServerResponse, next: NextFunction) {
if (req.url == "/kill") {
res.setHeader('Content-Type', 'text/plain');
res.writeHead(200);
res.end("Goodbye");
console.log("Received kill command");
process.exit();
}
else
next();
};
示例7: createServer
const server = createServer((req: IncomingMessage, res: ServerResponse) => {
const credentials = auth(req);
if (!credentials || credentials.name !== 'john' || credentials.pass !== 'secret') {
res.statusCode = 401;
res.setHeader('WWW-Authenticate', 'Basic realm="example"');
res.end('Access denied');
} else {
res.end('Access granted');
}
});
示例8: set
set(name: string, value: string, opts: Cookie = {}): this {
if (!this.res) {
throw new Error('You must specify at least: new Cookies(..., ServerResponse)')
}
const queuedHeader = asString(this.res.getHeader('Set-Cookie'))
const header = queuedHeader ? [queuedHeader] : []
header.push(serialize(name, value, opts))
this.res.setHeader('Set-Cookie', header)
return this // chainable
}
示例9: serverRequest
export function serverRequest(
settings: Partial<ReactConverterSettings>,
req: IncomingMessage,
res: ServerResponse
) {
const baseFolder = path.resolve(process.cwd(), process.env.DOCS || 'docs')
let url = req.url
if (!url) {
return
}
url = decodeURIComponent(url)
if (url.startsWith('/favicon.ico')) {
url = favicon
}
if (url.startsWith(ASSETS)) {
const filePath = path.join(__dirname, url.slice(ASSETS.length))
res.setHeader('Content-Type', mime.contentType(path.extname(filePath)))
return fs.readFileSync(filePath)
}
const urlPath = path.join(baseFolder, url)
if (fs.existsSync(urlPath) && fs.statSync(urlPath).isFile()) {
return fs.readFileSync(urlPath)
} else {
let indexPath = path.join(baseFolder, url, 'index.json')
if (fs.existsSync(indexPath)) {
const content = fs.readFileSync(indexPath).toString()
res.setHeader('Content-Type', 'text/html; charset=utf-8')
return renderHTML(JSON.parse(content).reflection, settings)
} else {
throw new Error(`File not found: ${url}`)
}
}
}
示例10:
cookies.forEach(data => {
let cookie = `${data.key}=${data.value}; `;
if (data.maxAge !== undefined)
cookie += `Max-Age = ${data.maxAge}; `;
if (data.domain !== undefined)
cookie += `Domain = ${data.domain}; `;
if (data.expires !== undefined)
cookie += `Expires = ${data.expires}; `;
if (data.path !== undefined) cookie += `Path = ${data.path}; `;
this.res.setHeader('Set-Cookie', cookie);
});