本文整理汇总了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);
}
};
示例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);
}
};
示例3: reject
const source = await new Promise<string>((resolve, reject) => fs.readFile(filePath, (err, source) => err ? reject(err) : resolve(source.toString())));