當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript https.ServerResponse類代碼示例

本文整理匯總了TypeScript中https.ServerResponse的典型用法代碼示例。如果您正苦於以下問題:TypeScript ServerResponse類的具體用法?TypeScript ServerResponse怎麽用?TypeScript ServerResponse使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了ServerResponse類的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: function

    return function (req: IncomingMessage, res: ServerResponse) {
        const url = req.url.replace(/\?.*/, '');

        if (url.includes('/package.json')) {
            res.end(readFileSync(join(__dirname, '..', 'package.json')));
        } else if (isTradingView(url)) {
            get(`https://client.wavesplatform.com/${url}`, (resp: IncomingMessage) => {
                let data = new Buffer('');

                // A chunk of data has been recieved.
                resp.on('data', (chunk: Buffer) => {
                    data = Buffer.concat([data, chunk]);
                });

                // The whole response has been received. Print out the result.
                resp.on('end', () => {
                    Object.keys(resp.headers).forEach((name) => {
                        if (name !== 'transfer-encoding' && name !== 'connection' && !res.getHeader(name)) {
                            res.setHeader(name, resp.headers[name]);
                        }
                    });
                    res.end(data);
                });
            });
            return null;
        }

        if (buildType !== 'dev') {
            if (isPage(req.url)) {
                const path = join(__dirname, '..', 'dist', type, connectionType, buildType, 'index.html');
                return readFile(path, 'utf8').then((file) => {
                    res.end(file);
                });
            }
            return routeStatic(req, res, connectionType, buildType, type);
        } else {
            if (buildType === 'dev' && req.url.includes('init.js')) {
                return getInitScript(connectionType, buildType, type).then((script) => {
                    res.end(script);
                });
            }
        }

        if (url.indexOf('/locales') === 0) {
            const [lang, ns] = url.replace('/locales/', '')
                .replace(/\?.*/, '')
                .replace('.json', '')
                .split('/');
            const cachePath = join(process.cwd(), '.cache-download', 'locale', lang, `${ns}.json`);

            if (existsSync(cachePath)) {
                const data = readFileSync(cachePath);
                res.end(data);
            } else {
                res.statusCode = 404;
                res.end('Not found!');
            }
            return null;
        }

        if (url.indexOf('/img/images-list.json') !== -1) {
            res.setHeader('Content-Type', 'application/json');
            const images = getFilesFrom(
                join(__dirname, '../src/img'),
                ['.svg', '.png', '.jpg'],
                (name, path) => path.indexOf('no-preload') === -1
            ).map(moveTo(join(__dirname, '../src')));
            res.end(JSON.stringify(images));
            return null;
        }

        if (isPage(url)) {
            return prepareHTML({
                target: join(__dirname, '..', 'src'),
                connection: connectionType,
                type,
            }).then((file) => {
                res.end(file);
            });
        } else if (isTemplate(url)) {
            readFile(join(__dirname, '../src', url), 'utf8')
                .then((template) => {
                    const code = minify(template, {
                        collapseWhitespace: true // TODO @xenohunter check html minify options
                    });
                    res.end(code);
                });
        } else if (isLess(url)) {
            const theme = req.url.match(/theme=(.+),?/)[1];

            readFile(join(__dirname, '../src', url), 'utf8')
                .then((style) => {
                    (render as any)(style, {
                        filename: join(__dirname, '../src', url),
                        paths: join(__dirname, `../src/themeConfig/${theme}`)
                    } as any)
                        .then(function (out) {
                            res.setHeader('Content-type', 'text/css');
                            res.end(out.css);
                        })
//.........這裏部分代碼省略.........
開發者ID:wavesplatform,項目名稱:WavesGUI,代碼行數:101,代碼來源:utils.ts

示例2:

 resp.on('end', () => {
     Object.keys(resp.headers).forEach((name) => {
         if (name !== 'transfer-encoding' && name !== 'connection' && !res.getHeader(name)) {
             res.setHeader(name, resp.headers[name]);
         }
     });
     res.end(data);
 });
開發者ID:wavesplatform,項目名稱:WavesGUI,代碼行數:8,代碼來源:utils.ts

示例3: minify

 .then((template) => {
     const code = minify(template, {
         collapseWhitespace: true // TODO @xenohunter check html minify options
     });
     res.end(code);
 });
開發者ID:wavesplatform,項目名稱:WavesGUI,代碼行數:6,代碼來源:utils.ts

示例4: getInitScript

 return getInitScript(connectionType, buildType, type).then((script) => {
     res.end(script);
 });
開發者ID:wavesplatform,項目名稱:WavesGUI,代碼行數:3,代碼來源:utils.ts

示例5: readFile

 return readFile(path, 'utf8').then((file) => {
     res.end(file);
 });
開發者ID:wavesplatform,項目名稱:WavesGUI,代碼行數:3,代碼來源:utils.ts


注:本文中的https.ServerResponse類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。