本文整理汇总了TypeScript中http.ServerResponse.write方法的典型用法代码示例。如果您正苦于以下问题:TypeScript ServerResponse.write方法的具体用法?TypeScript ServerResponse.write怎么用?TypeScript ServerResponse.write使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类http.ServerResponse
的用法示例。
在下文中一共展示了ServerResponse.write方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1:
server.sendRequest(baseUrl + request.url, pathname[2], (res: Host.Response) => {
response.writeHead(res.status, server.headers);
if (res.success) {
response.write(res.data);
server.setCache(request.url, res.data);
} else {
response.write(res.data + '\n');
}
response.end();
});
示例2: createServer
var newServer = createServer((req: any, res: ServerResponse)=>{
if (req.url =='/big.html')
{
}
res.write("hi. its sample");
res.end();
})
示例3: renderPage
export async function renderPage(
res: ServerResponse,
pathname: string,
query: ParsedUrlQuery,
tmpDir: string,
gaId: string,
) {
res.statusCode = getStatusCode(pathname);
res.write(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png" />
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png" />
<link rel="manifest" href="/site.webmanifest" />
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#33aa33" />
<meta name="msapplication-TileColor" content="#333333" />
<meta name="theme-color" content="#333333" />
<meta property="og:title" content="${title}" />
<meta property="og:image" content="https://${hostname}${pages.logo_png}" />
<meta property="og:description" content="${description}" />
<title>${title}</title>
<meta name="description" content="${description}" />
<style>${css}</style>
</head>
<body>
<div id="spinner"></div>
<script>document.getElementById('spinner').style.display='block'</script>
${OctocatCorner()}
<div id="${containerId}">`);
const factory = await routePage(pathname, query, tmpDir);
const stream = renderToNodeStream(factory);
stream.pipe(
res,
{ end: false },
);
stream.on('end', () => {
res.end(`</div>
<script>document.getElementById('spinner').style.display='none'</script>
<script type="text/javascript">
if (window.location.hostname === '${hostname}') {
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', '${gaId}', 'auto');
ga('send', 'pageview');
}
</script>
</body>
</html>`);
});
}
示例4:
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();
};
示例5:
const sse = (str: string) => {
if (isKoa) {
stream!.write(str);
} else {
res.write(str);
// support running within the compression middleware.
// https://github.com/expressjs/compression#server-sent-events
if (typeof (res as any).flushHeaders === 'function') (res as any).flushHeaders();
}
};
示例6: function
}).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();
});
示例7: finish
/**
* finish HTTP request
* @param status - HTTP status code
* @param headers - HTTP headers
* @param entiry - HTTP response entiry data
*/
private finish(
status: number,
headers: OutgoingHttpHeaders,
entiry?: Object
) {
let res = this.res;
headers = Object.assign({ 'Content-Type': 'text/html' }, headers);
res.writeHead(status, headers);
if (entiry !== undefined) res.write(entiry);
res.end();
}
示例8: done
req.on('end', () => {
const urlObject = url.parse(req.url, true);
const request = {
method: req.method,
body: body && JSON.parse(body),
path: req.url,
query: urlObject.query,
headers: req.headers,
};
const context = {
done(error, result) {
if (error) throw error;
res.statusCode = result.status;
for (let key in result.headers) {
if (result.headers.hasOwnProperty(key)) {
res.setHeader(key, result.headers[key]);
}
}
res.write(result.body);
res.end();
},
};
handler(context as any, request as any);
});
示例9: request
async function request (req: IncomingMessage, res: ServerResponse) {
const uri = url.parse(req.url).pathname
const filename = path.join(ROOT, uri)
res.setHeader('Content-Type', 'text/plain')
const status: Stat = await stat(filename)
if (status.mode & S.IFDIR) {
const dir: Dir = await opendir(filename)
for (let dirent of dir) {
res.write(dirent.name + '\0')
}
closedir(dir)
} else {
const file = await fopen(filename) as File
await file.pipeTo(res)
file.close()
}
switch (req.method) {
case 'GET':
}
}
示例10: handler
export default async function handler(req: IncomingMessage, res: ServerResponse) {
let { httpVersion, method, url } = req;
console.log(`${httpVersion} ${method} ${url}`);
if (!url || url === '/') {
url = 'index.html';
}
try {
if (url === 'index.html') {
res.setHeader('Content-Type', lookup(url));
res.setHeader('Cache-Control', control(isProd, 1));
res.write(`<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="${faviconUrl}" rel="icon" type="image/x-icon" />
<title>React Example</title>
<link rel="stylesheet" href="${stylesUrl}" />
</head>
<body>
<div id="${containerId}">`);
const stream = renderToNodeStream(AppFactory(fetchProps()));
stream.pipe(
res,
{ end: false },
);
stream.on('end', () => {
res.end(`</div>
<script src="https://unpkg.com/react@${reactVersion}/umd/react${suffix}"></script>
<script src="https://unpkg.com/react-dom@${reactDomVersion}/umd/react-dom${suffix}"></script>
<script src="${browserUrl}"></script>
</body>
</html>`);
});
} else if (url === propsUrl) {
res.setHeader('Content-Type', lookup(url));
res.setHeader('Cache-Control', control(isProd, 0));
res.end(JSON.stringify(fetchProps()));
} else if (url === stylesUrl) {
res.setHeader('Content-Type', lookup(url));
res.setHeader('Cache-Control', control(isProd, 7));
const file = `./src/${url}`;
createReadStream(file).pipe(res);
} else if (url === browserUrl || url === browserMapUrl) {
res.setHeader('Content-Type', lookup(url));
res.setHeader('Cache-Control', control(isProd, 7));
const file = `./dist${url}`;
createReadStream(file).pipe(res);
} else {
url = 'notfound.txt';
res.setHeader('Content-Type', lookup(url));
res.setHeader('Cache-Control', control(isProd, 0));
res.statusCode = 404;
res.end('404 Not Found');
}
} catch (e) {
console.error(e);
url = 'notfound.txt';
res.setHeader('Content-Type', lookup(url));
res.setHeader('Cache-Control', control(isProd, 0));
res.statusCode = 500;
res.end('500 Internal Error');
}
}