Can i use nestjs services in expressjs ?
- User
Can i use nestjs services in expressjs ?
Assistant
Yes, you can absolutely use NestJS services in an Express app! There are a few ways to do this:
- Import the service directly:
import { FooService } from './foo.service'; const app = express(); app.get('/foo', (req, res) => { const fooService = new FooService(); const result = fooService.doSomething(); res.send(result); });- Import the NestFactory and create a Nest app, then use its services:
import { NestFactory } from '@nestjs/core'; import { FooModule } from './foo.module'; const app = express(); const nestApp = await NestFactory.create(FooModule); const fooService = nestApp.get(FooService); app.get('/foo', (req, res) => { const result = fooService.doSomething(); res.send(result); });- Create a Nest "microservice" and call its API from Express:
// foo.module.ts @Module({ providers: [FooService] }) export class FooModule {} // main.ts const nestApp = await NestFactory.create(FooModule); await nestApp.listen(3001); // express.js const app = express(); app.get('/foo', (req, res) => { axios.get('http://localhost:3001/foo') .then(response => { res.send(response.data); }); });So in summary, yes you have a few options for using NestJS services in your Express app! Let me know if you have any other questions.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.