Reputation: 29129
I think I can say I'm a bit of a noob with microservices. So, which is why I wanted to play with it. I used NestJs, because it looked easy
First I created a new app with nest new myservice
Then I copied from the microservice docs the example main.ts
and controller.ts into the project:
main.ts
:
import { NestFactory } from '@nestjs/core';
import { Transport } from '@nestjs/microservices';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.createMicroservice(AppModule, {
transport: Transport.TCP,
options: { host: 'localhost', port: 3005 },
});
app.listen(() => console.log('Microservice is listening'));
}
bootstrap();
app.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
imports: [],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {
controoler.ts
import { Controller } from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';
@Controller()
export class AppController {
@MessagePattern({ cmd: 'sum' })
accumulate(data: number[]): number {
return (data || []).reduce((a, b) => a + b);
}
}
Now when I start it, all looks well:
✗ yarn start
yarn run v1.13.0
$ ts-node -r tsconfig-paths/register src/main.ts
[Nest] 45783 - 05/01/2019, 11:08 PM [NestFactory] Starting Nest application...
[Nest] 45783 - 05/01/2019, 11:08 PM [InstanceLoader] AppModule dependencies initialized +17ms
[Nest] 45783 - 05/01/2019, 11:08 PM [NestMicroservice] Nest
microservice successfully started
Microservice is listening
So, if anything is wrong here, please let me know! But know I would like to write a small test nodejs app that can call/communicate with this microservice. Any suggestion where to start with that. Can I use axios for example or should I use something else. Any help would be appreciated!
Upvotes: 3
Views: 4385
Reputation: 76
I tried to communicate express app with nodejs microservice following the last post (Aleksandr Yatsenko) and works OK only if you install on your express app:
Here is the code:
const express = require('express')
const app = express()
const port = 3002
const { ClientTCP } = require('@nestjs/microservices');
const { lastValueFrom } = require('rxjs');
(async () => {
const client = new ClientTCP({
host: 'localhost',
port: 3001,
});
await client.connect();
app.get('/', async (req, res) => {
const pattern = { cmd: 'sum' };
const data = JSON.parse(req.query.data)
const result = await lastValueFrom(client.send(pattern, data))
res.json({ result })
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
})();
Upvotes: 1
Reputation: 869
You need to do the following.
import { ClientTCP } from '@nestjs/microservices';
(async () => {
const client = new ClientTCP({
host: 'localhost',
port: 3005,
});
await client.connect();
const pattern = { cmd: 'sum' };
const data = [2, 3, 4, 5];
const result = await client.send(pattern, data).toPromise();
console.log(result);
})();
Upvotes: 9