Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-1· AnthropicHugging Face Arena Human Preference 55K

Can i use nestjs services in expressjs ?

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-2.1’s answer.
  1. User

    Can i use nestjs services in expressjs ?

  2. Claude Assistant

    Yes, you can absolutely use NestJS services in an Express app! There are a few ways to do this:

    1. 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);
    });
    
    1. 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);
    });
    
    1. 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.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.