Example #1
0
  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);
    }
  };
Example #2
0
  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);
    }
  };
Example #3
0
 const source = await new Promise<string>((resolve, reject) => fs.readFile(filePath, (err, source) => err ? reject(err) : resolve(source.toString())));