@alt-javascript/camel-lite-component-file
v1.1.1
Published
[](https://opensource.org/licenses/MIT)
Downloads
357
Maintainers
Readme
What
File read (consumer: polls a directory) and write (producer: writes exchange body to a file). Supports configurable file name, character encoding, and auto-delete after read.
Install
npm install camel-lite-component-fileURI Syntax
file:/absolute/path[?fileName=out.txt&charset=utf8&delete=false]| Parameter | Default | Description |
|------------|-------------|-------------|
| fileName | (auto) | Output file name for producers. Consumers ignore this; all files in the directory are read. |
| charset | utf8 | Character encoding for reading/writing file content. |
| delete | false | Consumer only: delete the source file after it has been successfully read. |
Usage
Producer — write exchange body to a file:
import { CamelContext } from 'camel-lite-core';
import { FileComponent } from 'camel-lite-component-file';
const context = new CamelContext();
context.addComponent('file', new FileComponent());
context.addRoutes({
configure(ctx) {
ctx.from('direct:writeFile')
.to('file:/tmp/output?fileName=result.txt&charset=utf8');
}
});
await context.start();
const template = context.createProducerTemplate();
await template.sendBody('direct:writeFile', 'Hello, file!');
await context.stop();Consumer — read files from a directory:
context.addRoutes({
configure(ctx) {
ctx.from('file:/tmp/inbox?delete=true')
.process(exchange => {
console.log('File content:', exchange.in.body);
console.log('File name:', exchange.in.getHeader('CamelFileName'));
});
}
});