Comments (13)
@thomrick He's asking that the examples on https://kamilmysliwiec.gitbooks.io/nest/content/ add the Express types. Right now they just say:
@Get('/:id')
public async getUser(@Response() res, @Param('id') id) {
as opposed to
@Get('/:id')
public async getUser(@Response() res: Response, @Param('id') id) {
from nest.
Then the server package would still need to have both Fastify and Nest platform installed for it to work, and Nest still supports JS, so that's not an option either, plus it's more a downgrade than an actual bug fix imo.
My advice? Stop being lazy.
from nest.
To solve the problem:
npm install --save-dev @types/express
I think we can close this issue.
from nest.
unfortunately not possible to import Request, Response
from @types/express
and nest.js
together, have to alias one of them
import { Response, Request } from '@types/express';
import {
Response as Req,
Request as Res
} from 'nest.js';
from nest.
When trying to do the above I get:
src/index.ts(1,8): error TS1192: Module '"C:/Development/me/nest-graph-api/node_modules/@types/express/index"' has no default export.
src/users/users.controller.ts(11,18): error TS4053: Return type of public method from exported class has or is using name 'Response' from external module "C:/Development/me/nest-graph-api/node_modules/@types/express-serve-static-core/index" but cannot be named.
src/users/users.controller.ts(17,18): error TS4053: Return type of public method from exported class has or is using name 'Response' from external module "C:/Development/me/nest-graph-api/node_modules/@types/express-serve-static-core/index" but cannot be named.
src/users/users.controller.ts(23,18): error TS4053: Return type of public method from exported class has or is using name 'Response' from external module "C:/Development/me/nest-graph-api/node_modules/@types/express-serve-static-core/index" but cannot be named.
Here is the code I'm trying to transpile
import { Response, Request } from '@types/express';
import { Controller, Get, Post, Request as Req, Response as Res, Param, Body, HttpStatus } from 'nest.js';
import { UsersService } from './users.service';
@Controller('users')
export class UsersController {
constructor(private usersService: UsersService) {}
@Get()
public async getAllUsers(@Res() res: Response) {
const users = await this.usersService.getAllUsers();
return res.status(HttpStatus.OK).json(users);
}
@Get('/:id')
public async getUser(@Res() res: Response, @Param('id') id) {
const user = await this.usersService.getUser(id);
return res.status(HttpStatus.OK).json(user);
}
@Post()
public async addUser(@Res() res: Response, @Body('user') user) {
const msg = await this.usersService.addUser(user);
return res.status(HttpStatus.CREATED).json(msg);
}
}
This was addressed by #36
from nest.
Since latest version @Request()
and @Response()
decorators have an aliases - @Req()
and @Res()
so there is no collision with express typings. About @types/express
, I added short notice about it in the documentation.
from nest.
I found an issue with decorators being used on function parameters that might be related to this.
when using something like
@Get('playlist')
async getPlaylist(@Res() res) {
res.send(playlist);
}
This will only work so long as your typescript's tsconfig.json is targeting es2015 or higher, if targeting es5, this will throw an error like so:
[Nest] 17957 - 2017-8-2 22:14:37 [ExceptionsHandler] res.send is not a function
TypeError: res.send is not a function
at PlaylistController.<anonymous> (/Users/mark.mccracken/Work/learning/angular/state-management/dist/server/playlist/playlist.controller.js:118:43)
at step (/Users/mark.mccracken/Work/learning/angular/state-management/dist/server/playlist/playlist.controller.js:44:23)
at Object.next (/Users/mark.mccracken/Work/learning/angular/state-management/dist/server/playlist/playlist.controller.js:25:53)
at /Users/mark.mccracken/Work/learning/angular/state-management/dist/server/playlist/playlist.controller.js:19:71
at Promise (<anonymous>)
at __awaiter (/Users/mark.mccracken/Work/learning/angular/state-management/dist/server/playlist/playlist.controller.js:15:12)
at PlaylistController.getPlaylist (/Users/mark.mccracken/Work/learning/angular/state-management/dist/server/playlist/playlist.controller.js:116:16)
at /Users/mark.mccracken/Work/learning/angular/state-management/node_modules/@nestjs/core/router/router-proxy.js:7:33
at Layer.handle [as handle_request] (/Users/mark.mccracken/Work/learning/angular/state-management/node_modules/@nestjs/core/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/mark.mccracken/Work/learning/angular/state-management/node_modules/@nestjs/core/node_modules/express/lib/router/route.js:131:13)
::1 - - [02/Aug/2017:21:14:37 +0000] "GET /api/playlist HTTP/1.1" 500 31
Hope this might help anyone coming behind me with a really simple solution
from nest.
This will only work so long as your typescript's tsconfig.json is targeting es2015 or higher, if targeting es5, this will throw an error like so...
This is happening to me also with ES6 in tsconfig.json.
from nest.
When it was said that this framework works in TypeScript I expected it to support express types integrally. For example - I don't need to import the express module because NestJS does that for me. Why than would I need to import express types? The types should be imported and inferred automatically when I use the @Req decorator.
from nest.
@ShacharHarshuv that's not how decorators work in TS.
Even if they could infer with return types, there's still an issue, because Nest supports both Fastify and Express.
from nest.
What if I'll just need to write the type (e.g. Request) without a decorator and Nest will give me the request like happens with dependency injection? (You ask for a class and the injector supplies you with an instant.)
Is that something that might be possible to implement?
from nest.
Any update on this? I still face the same issue when targeting es2017. Using Nest 6.5.3
from nest.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
from nest.
Related Issues (20)
- Hyphens in parameters silently fail HOT 4
- Improve documentation on @Injectable() HOT 2
- Log is not auto-flushed on error during initialization HOT 1
- Middleware not executed when using `exclude` in `setGlobalPrefix` HOT 1
- The use of SetMetadata custom decorators does not take effect HOT 13
- NestJS HttpException class returns success status when status is undefined HOT 4
- NestJs DI `useFactory` ignores property dependencies HOT 1
- I can't use two kafka client in one nest.js server to consume other topics. HOT 1
- Support using `@Inject()` decorator factory with no args on constructor-based injections
- Add CLI options to make the testing, linting, and formatting configurations optional. HOT 1
- @nestjs/microservices: RabbitMQ connection options not passed to amqp-connection-manager HOT 2
- 我有一个问题,关于依赖注入和模块导入的 HOT 1
- [chore] drop dev. dependencies that are leading to errors on running `npm install` in this repo HOT 6
- Support for Multiple HTTP Methods on the function HOT 3
- Cannot read properties of undefined (reading 'constructor') HOT 3
- Nest can't resolve dependencies of the DiscoveryService (?). Please make sure that the argument ModulesContainer at index [0] HOT 11
- I have a question about dependency injection and module import HOT 1
- Supply time to TypeOrmLogger.logQuery() HOT 1
- TCP.onStreamRead (node:internal/stream_base_commons:217:20) HOT 1
- RabbitMQ Bug Microservices HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nest.