0

I'm learning Node.js along with Angular (a MEAN project) via Udemy, where I've to create identical model for both back-end and front-end(angular2). It makes me wonder if there's a way to create a model that somehow will apply for both sides?

If I need to change a class, then I have to make changes in both, which could have a possibility for missing out a thing or two in the long term.

ipid
  • 253
  • 1
  • 4
  • 16
  • You're asking if you can have both a *Front-End* and a *Back-End* accessible to the same Model Schema definition? – Tobiah Rex Jan 29 '18 at 00:23
  • You can have a local dependency used by both front and back, that contains your models interfaces. See: https://www.google.fr/url?sa=t&source=web&rct=j&url=https://stackoverflow.com/questions/15806241/how-to-specify-local-modules-as-npm-package-dependencies&ved=2ahUKEwiBkLzd9fvYAhUCORQKHepxAEoQjjgwAHoECBEQAQ&usg=AOvVaw1jSengeoq8C8FnlZpExxFW – Maxime Flament Jan 29 '18 at 00:29
  • @TobiahRex Yes, that's correct. – ipid Jan 29 '18 at 00:58

1 Answers1

1

I've already had the same question months ago and my answer was: no. The reasons:

  1. Models are Angular is written in .ts files and the server, in .js. I thought this difference between extension could lead future problems.

  2. Both front-end (Angular) and backend (Express) should have to share the same folder. I didn't like this idea to put both in the same folder because the project structure would be too large and complex to manage.

  3. I was using mongoose. Mongoose schema/model declaration is different from an Angular class declaration, they're incompatible with each other.

These were my consideration about sharing the same file model.

Pedro Arantes
  • 5,113
  • 5
  • 25
  • 60
  • But now node.js framework `nest.js` give you typescript version of node.js so you cna keep your models in a shared folder from where both front-end and back-end can use those models – Fahad Subzwari Oct 11 '19 at 14:44
  • @FahadSubzwari , im trying to do the same at this moment... plz advise if u have any ideas in another answer – Khaleel Hmoz Nov 08 '20 at 15:08