当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript graceful-fs.readFile函数代码示例

本文整理汇总了TypeScript中graceful-fs.readFile函数的典型用法代码示例。如果您正苦于以下问题:TypeScript readFile函数的具体用法?TypeScript readFile怎么用?TypeScript readFile使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了readFile函数的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: expressEngine

  return function expressEngine(filePath: string, data: any = {}, done?: Function) {
    // defaults
    data = data || {};
    var cancel = false;
    const resConfig = Object.assign(data, {
      platformRef,
      cancelHandler: () => cancel,
      time: _options.time,
      asyncDestroy: _options.asyncDestroy,
      id: _options.id()
    });

    var req: any = (data.req && data.req.on && data.req) ||
    (data.request && data.request.on && data.request);

    req.on('close', () => cancel = true);


    function readContent(content) {
      const document: string = content.toString();
      resConfig.document = document;

      // convert to string

      return (_options.precompile ?
        platformRef.serializeModule(_options.main(resConfig), resConfig) :
        platformRef.serializeModuleFactory(_options.mainFactory(resConfig), resConfig)
      )
        .then(html => {
          done(null, html);
        })
        .catch(e => {
          console.error(e.stack);
          // if server fail then return client html
          done(null, document);
        });
    }

    // read file on disk
    try {

      if (cache[filePath]) {
        return readContent(cache[filePath]);
      }
      fs.readFile(filePath, (err, content) => {
        if (err) {
          cancel = true;
          return done(err);
        }
        cache[filePath] = content;
        return readContent(content);
      });

    } catch (e) {
      cancel = true;
      done(e);
    }
  };
开发者ID:jeffwhelpley,项目名称:universal,代码行数:58,代码来源:index.ts

示例2: expressEngine

  return function expressEngine(filePath: string, data: ExpressEngineConfig = {ngModule: _options.ngModule}, done?: Function) {
    const ngModule = data.ngModule || _options.ngModule;
    if (!ngModule) {
      throw new Error('Please provide your main module as ngModule for example res.render("index", {ngModule: MainModule}) or in the engine as createEngine({ ngModule: MainModule })')
    }
    if (!data.req || !data.res) {
      throw new Error('Please provide the req, res arguments (request and response objects from express) in res.render("index", { req, res })');
    }
    var cancel = false;
    if (data.req) {
      data.req.on('close', () => cancel = true);
    }
    // defaults
    const _data = Object.assign({
      get cancel() { return cancel; },
      set cancel(val) { cancel = val; },

      get requestUrl() { return data.requestUrl || data.req.originalUrl },
      set requestUrl(val) {  },

      get originUrl() { return data.originUrl || data.req.hostname },
      set originUrl(val) {  },

      get baseUrl() { return data.baseUrl || '/' },
      set baseUrl(val) {  },

      get cookie() { return data.cookie || data.req.headers.cookie },
      set cookie(val) {  },
    }, data);

    function readContent(content) {
      const DOCUMENT: string = content.toString();
      // TODO(gdi2290): breaking change for context globals
      // _data.document = parseDocument(document);
      _data.document = DOCUMENT;
      _data.DOCUMENT = DOCUMENT;
      _data.cancelHandler = () => Zone.current.get('cancel');

      const zone = Zone.current.fork({
        name: 'UNIVERSAL request',
        properties: _data
      });



      // convert to string
      return zone.run(() => (_options.precompile ?
        platformRef.serializeModule(ngModule, _data) :
        platformRef.serializeModuleFactory(ngModule, _data)
      )
        .then(html => {
          if (typeof html !== 'string' || cancel) {
            return done(null, DOCUMENT);
          }
          done(null, html);
        })
        .catch(e => {
          console.log(e.stack);
          // if server fail then return client html
          done(null, DOCUMENT);
        }));
    }

    // read file on disk
    try {

      if (cache[filePath]) {
        return readContent(cache[filePath]);
      }
      fs.readFile(filePath, (err, content) => {
        if (err) {
          cancel = true;
          return done(err);
        }
        cache[filePath] = content;
        return readContent(content);
      });

    } catch (e) {
      cancel = true;
      done(e);
    }
  };
开发者ID:jeckhart,项目名称:universal,代码行数:83,代码来源:index.ts

示例3: reject

 const source = await new Promise<string>((resolve, reject) => fs.readFile(filePath, (err, source) => err ? reject(err) : resolve(source.toString())));
开发者ID:jspm,项目名称:jspm-cli,代码行数:1,代码来源:dew-worker.ts


注:本文中的graceful-fs.readFile函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。