16
u/gentritabazi01 Mar 10 '21
8
u/BenjiSponge Mar 10 '21
It bothers me that people are downvoting this comment because they don't like it.
Downvoting is not "I don't like this". Downvoting is "this should be lower". This comment should be pinned to the top because it's addendum to the post. It would be perfectly valid reddiquette to downvote the post and upvote this comment.
→ More replies (4)
57
u/AmineTKH Mar 10 '21
Pretty much spring boot
4
u/_Pho_ Mar 12 '21
Full circle. These route annotations are chasing me across languages at this point.
1
53
u/yellow_magician Mar 10 '21
is this Spring MVC?
16
u/Faheemify Mar 10 '21 edited Mar 10 '21
Nest.JS, pretty sure.I am wrong just saw the code and did not read the imports.See child comment.
→ More replies (2)1
107
u/llldar Mar 10 '21
If you like these you might as well just write c#
50
9
u/npmbad Mar 10 '21
You got me thinking now, I really miss structure and runtime type checking. How's .net core servers compared to nodejs in general?
8
u/Veranova Mar 10 '21
ASP Core is great and very similar to this code example. C# struggles with a lot of the magic that JS does very well though, and there are benefits to sharing API data types to the frontend. But C# does stop data leaking in and out of the API which isn’t on the types, due to the runtime type mapping. Pros and cons.
I personally would love a well structured and declarative web framework like this for node as you could get the best of both worlds. I’ve found express to get a bit creaky with typescript, though it’s still fine today.
→ More replies (3)4
6
→ More replies (1)4
38
u/Zachincool Mar 10 '21
It only looks clean because your methods aren't doing anything
→ More replies (5)
36
u/Skhmt Mar 10 '21
Is this even JS anymore?
22
0
Mar 10 '21
[deleted]
9
u/HetRadicaleBoven Mar 10 '21
Care to share what you think has changed to make that no longer (roughly) true?
→ More replies (30)7
u/cbadger85 Mar 10 '21
I think you might be to look up the definition of superset.
→ More replies (9)
6
u/EntroperZero Mar 11 '21 edited Mar 11 '21
You've made ASP.NET Web API in Node.
This amuses me, because the .NET guys are trying to make Express/Koa in .NET. :)
EDIT: I found an example of what I'm talking about.
132
u/Adoroam Mar 10 '21
decorators and classes are the worst thing to happen to js. i find nothing about that too be "clear". this is not the direction forward. i'm sure it works great, but i hate it. 😐
65
35
u/geon Mar 10 '21
Looks like something a java programmer would write. What is the purpose of the class? What state does it contain? Nothing!
The class is there just because the programmer likes to put code in classes. Not because it actually makes the code better in any way.
→ More replies (7)6
u/lord-apple-smithe Mar 10 '21
This is so true and a pattern i lament in typescript projects, just because you have classes doesn't make them mandatory
→ More replies (1)20
u/way-okay Mar 10 '21
I've always thought you should mainly aim to use the conventions in place in the language's ecosystem.
For example Kotlin and JavaScript both support classes and first-class functions. But if I am writing Kotlin I tend to favour classes and if I am writing Javascript I tend to favour functions, objects, and singletons.
The number of Typescript projects I have come across in the last few years where rather than enhancing JavaScript they dump all it's conventions and act like it is a c# project.
6
u/conventionalWisdumb Mar 10 '21
Agreed. I kind of hate classes in JavaScript, I’d rather use closures that return objects to get class-like behavior. There’s not a ton of cases where classical inheritance is really more useful and you run the risk of having a rigid architecture when you do use it.
-9
u/queen-adreena Mar 10 '21
That’s basically what Typescript is: an attempt to turn JavaScript into a different language.
6
u/romeeres Mar 10 '21
Different, but better! Personally I switched to node from ruby because I get tired of runtime errors
3
u/folkrav Mar 11 '21
Eh, not really. It's nothing but JavaScript w/ compile time type-checking and some extra sugar. The choice to use classes and decorators is just that - a choice. Seriously, I barely write any type hints outside function definitions, use interfaces instead of plain undocumented JS objects, and basically never use classes, and TS figures out the rest. Just using TypeScript and putting a handful of hints left and right gets rid of so many runtime errors and manual type checking boilerplate it's IMHO honestly quite idiotic not to use it for anything you expect to maintain longer term and/or with larger teams.
26
6
u/too_much_exceptions Mar 10 '21
I have a different opinion on decorators.
They really help dealing with cross cutting concerns in an elegant way and improving code legibility.
Using decorators sure needs discipline. They can « hide » code execution paths, and add « magic » to code: this can be anti patterns if used everywhere in the code.
2
u/crowdyriver Mar 10 '21
they include classes but still no pipe operator.
God damn JS would improve so much just with that
→ More replies (1)2
18
u/Dan6erbond Mar 10 '21
I like the look of this but personally I prefer NestJS which is what I have been using for ages now. I also wrote a blog post on my setup.
12
u/s_boli Mar 10 '21
At first glance this looks a lot like NestJS.
I agree. If you're gonna go that route, you might aswell use the full thing to get all the benefits.→ More replies (4)
4
u/connormcwood Mar 10 '21
When I first saw decorators in js I thought about doing something similar to this, reminds me so much of Springboot. Can see the pros and cons of either, atleast it’ll hide a lot of boiler plate. At this point might as well just use Java
15
6
u/cheeseisakindof Mar 11 '21
Tbh I find this to be really convoluted code. You could write something with the same functionality in about 15 lines of Typescript + Express.
3
3
12
u/brianjenkins94 Mar 10 '21 edited Mar 10 '21
I’m pretty sure return await is pointless here.
57
u/danjordan Mar 10 '21
51
25
u/sivadneb Mar 10 '21
I did not not know this. Missing stack traces are the bane of my existence. Thanks!
2
1
1
u/captmomo Mar 11 '21
Thanks for this! I’ve been doing return await as I came from a c# background, and eslint kept flagging me, guess I now have a reason to disable it.
0
Mar 10 '21
[deleted]
5
u/leodriesch Mar 10 '21
The article talks about a stack traces in v8, which is also used as the JavaScript engine behind new Edge, Chrome and more, so yes.
3
u/fatty1380 Mar 10 '21
Depends on what the decorators do. If they add error handling,
return awaitis exactly what you need to do. Returning a promise from within a try block will not work like you want it to
7
u/JohnLouderback Mar 10 '21
I love the decorators approach. Being able to abstract away complex behavior or add metadata and just imbue your classes with them is such a godsend.
5
u/darrenturn90 Mar 10 '21
Have you seen composition in functional programming ? It’s like decorators but without the extra symbology
1
u/JohnLouderback Mar 10 '21
Absolutely! Love that too. Really, they're the same except for, as you said, the syntax, just used in different paradigms. To me, it's great to have the flexibility to use both or either depending on the needs of the project. Functional programming is an awesome paradigm, no arguments there. Decorators make working with object oriented class-based code so much more flexible. The ability to do meta programming with classes with a clear and concise syntax is a huge boon for OOP code.
2
2
2
u/npmbad Mar 10 '21
This looks cool but this is only ~5% of the code. The real structure should happen in the imported modules because that's where everything is actually happening.
2
2
2
u/NoInkling Mar 11 '21
I like the method decorators, not so sure about the parameter or class decorators. Especially routing being handled at the controller level, I prefer to do that elsewhere.
2
8
4
u/Hagaromo83 Mar 10 '21
I am using NestJs(99.9% sure that NestJs is in picture) as my main framework at current job, and it was never so easy to work on a backend as a bigger team. The scalability and infrastructure is great in NestJs. (Sorry for my english)
4
u/WorriedEngineer22 Mar 10 '21
Surprisingly, it's not NestJS. It seems a library that allows you to create controllers the way they do
4
u/Hagaromo83 Mar 10 '21
Yeah, i didnt check the imports :). You were right, i am surprised its not nestjs.
3
2
u/durandj Mar 10 '21 edited Mar 10 '21
Those ../ imports though...
I really recommend replacing those with something more maintainable like an alias.
Edit:
For the people down voting without explaining, once you work on a large code base with other people you'll find that relative imports are a pain.
It's unclear at a glance where the thing you're importing actually is. You have to keep more information in your head while reviewing this kind of code which means you're more likely to be wrong.
You're probably going to have to have way more changes in your diff when doing a simple refactor such as moving a file around in it's hierarchy. This is a problem because it increases your chances of a merge conflict and means more time spent by reviewers.
If you have some file names that are common to multiple modules in your code then you might end up with more code changes required to manually fix the now broken imports or the import works but the functionality will change in non-obvious ways. If you have the right tooling all the time some of this is taken care of for you but you better have tests to back things up.
2
u/romeeres Mar 10 '21
Why to use alias when you can simply add "basePath": "src" to tsconfig?
And then just use imports like "import { User } from "user/model" for example.Aliases are helpful when you need to import from directory located outside, for example, I have "client", "server" and "shared" dirs in project, and I'm importing shared files as "import Something from '@shared/something'"
1
u/durandj Mar 10 '21
If you're not using Webpack then what you're saying is completely valid.
If you are using Webpack you need to also have an alias.
I was using alias as a really high level term to cover all of these term variations.
1
u/romeeres Mar 10 '21 edited Mar 10 '21
If you are using Webpack you need to also have an alias.
With or without webpack it works well.
I'm using baseUrl in nestjs project which is using webpack.
The same baseUrl works with webpack for frontend projects, no aliases needed at all.I was using alias as a really high level term to cover all of these term variations.
Which term variations do you mean?
With baseUrl you can import x from "something/x"With alias you can import the same x from "@something/x" - how is it better?If you write project with plain js - then you can set env variable NODE_PATH=src for the same purpose.
Btw, I don't understand why people are using webpack, typescript by itself can transpile code for older node version, and webpack is heavy tool which increases compile time
→ More replies (5)1
0
Mar 10 '21
[deleted]
2
u/durandj Mar 10 '21
You have to keep more information in your head about which paths are local code or not.
The way I solve this is the alias is always the name of the current package. If the import includes my package name then it's local. I don't subscribe to the having too many aliases school that some people do.
Why do you have to keep more information in your head while reviewing relative imports?
I have to keep track of the file path that I'm currently reviewing as well as the other files in the related directories and then apply this relative path onto that map. For smaller projects that's fine but as the number of possible import paths increases it gets harder.
there is nothing that will cause a merge conflict about this
If I move a file which causes the import path to change and someone else adds some other named import on the same line then that is a conflict.
Why would having common file names break imports? And how would aliases fix that?
The most trivial example I can come up with right now is a utils file. If you have a common pattern for how all modules in your project should be structured and one rule says that all extra functions go into a utils file (awful practice but I've seen it) then moving a file up or down the hierarchy might still resolve
../utilsbut be resolving to different files.→ More replies (5)0
Mar 10 '21
[deleted]
7
u/Greenimba Mar 10 '21
Aliases are in the tsconfig file and are essentially paths to folders. So instead of "../../../../services/myService", you have an alias @services which points to the services folder, and then you do @services/myService.
Makes moving files a lot easier, as the imports stay the same.
2
u/n1ru4l Mar 10 '21
this can also be achieved by using npm modules and having a pavkage.json. Also moving stuff around should update relative paths if you are using a editor such as vscode.
3
u/durandj Mar 10 '21
For pure Typescript see this documentation: https://www.typescriptlang.org/tsconfig#paths
For Webpack see here: https://www.typescriptlang.org/tsconfig#paths
1
1
1
1
u/lapzap98 Mar 11 '21
look like you have async return await pattern.it doesn't make sense to make function async and then have return await. just simply return Promise
1
u/BlueCigarIO Mar 11 '21
Only thing your missing is to redo your imports to not use “..” but rather use the complete package/file filepath. That would definitely help make it look even cleaner
1
1
1
u/brulla Mar 11 '21
I’d prefer to use one-liners with func = () => without using the return key-word
1
u/Capaj Mar 11 '21
I'll take Graphql over this any day, because unless this generates documentation too it's still a hassle to change anything.
1
u/daftmaple Mar 14 '21
As a Spring developer, I love that I have the option to put decorators in TS. I understand that some people might not like it, but having the flexibility to do both FP and OOP means that you can do your style of programming in one language and I like it that way.
1
1
Mar 11 '21
Learning of TypeDI was a eye opener, it makes a lot easier to use a TDD approach and use good design patterns.I usually don't use for regular APIs, since I believe the stronger side of js shows up using a funcional approach. Right now i'm working in a Graphql API, using type-graphql, and the way it work together TypeDI is just awesome.
And for the people saying this is wrong because this guy is 'just writing c#' or 'spring ', it's crazy how amateur it seems. JS is not good or worse than C++ or C# because the way you structure your code, language/ecosystem should only metter when you are talking about system requirements, project needs and skillset of programmer. Just because you are writting JS you are not required to throw away SOLID principles, design patterns or programming paradigms.
-1
0
-2
u/romeeres Mar 10 '21
Why no one follows "structure by components" suggestion from node best practices? No one ever googled node best practices? That structure by components is rule #1.
1
0
-2
u/Miridius Mar 10 '21
This file does nothing except glue other files together, it has essentially no logic of its own. You posted 45 lines of boilerplate
-3
0
0
0
45
u/StoneCypher Mar 10 '21
If you put it up as code, rather than a screenshot, people could easily show you how they would do it instead
This looks like it might be 8 lines of standard code but I can't tell because the nature of
resourceOptionsis hidden and I don't know what half these magic annotations actually do