Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@nestjs/common

Package Overview
Dependencies
Maintainers
1
Versions
368
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nestjs/common

Nest - modern, fast, powerful node.js web framework (@common)

  • 9.0.11
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4M
increased by10.77%
Maintainers
1
Weekly downloads
 
Created

What is @nestjs/common?

The @nestjs/common package is a core part of the NestJS framework, which is a framework for building efficient, reliable and scalable server-side applications. This package provides a wide range of basic functionalities necessary for any NestJS application, including decorators, modules, middleware, filters, pipes, guards, and utilities for handling HTTP requests and responses.

What are @nestjs/common's main functionalities?

Decorators

Decorators are used to annotate classes and their members to define their roles within the application. For example, `@Injectable()` marks a class as a service that can be injected, and `@Controller()` defines a class as a controller with a specific route.

@Injectable()
class MyService {}

@Controller('my-route')
class MyController {
  constructor(private myService: MyService) {}
}

Modules

Modules are used to organize components by encapsulating them within bounded contexts. This code sample demonstrates how to define a module that includes controllers and providers.

@Module({
  imports: [],
  controllers: [MyController],
  providers: [MyService],
})
class MyModule {}

Middleware

Middleware are functions that execute during the request-response cycle. They can perform tasks like logging, request validation, etc. This code sample shows a basic middleware that logs a message on each request.

@Injectable()
class MyMiddleware implements NestMiddleware {
  use(req: Request, res: Response, next: NextFunction) {
    console.log('Request...');
    next();
  }
}

Exception Filters

Exception filters handle exceptions that occur during the processing of a request. This code sample demonstrates a filter that catches HTTP exceptions and formats the response.

@Catch(HttpException)
class HttpExceptionFilter implements ExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse<Response>();
    const status = exception.getStatus();

    response
      .status(status)
      .json({
        statusCode: status,
        timestamp: new Date().toISOString(),
        path: ctx.getRequest<Request>().url,
      });
  }
}

Other packages similar to @nestjs/common

FAQs

Package last updated on 16 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc