How to set up references and import external modules as top-level objects in Typescript in Node.js -


so typescript file structure , naming question. i'm starting learn typescript classes , modules , have few questions on how structure model logic across multiple files.

say have small rest api written in node.js user , photo models, , it's simple api lets users register , upload photo.

assuming file structure:

-app    -models       -user.js       -photo.js    -controller.js 
  1. in controller better use reference path declaration or external modules when referencing models? overlap using standard node.js require statement?

  2. if use import statement load models external module, can change below example move export top-level can new user() rather models.user().

for example, models, , anothermodel seems bit redundant - , don't want combine models 1 file.

controller.js

import models = require('./models/user'); import anothermodel = require('./models/photo'); var newuser = new models.user(); var newmoment = new anothermodel.moment(); 

user.js

export class user {   name:string } 

photo.js

export class photo {   url:string } 

you can create model / index.ts , re-export models file easier consumption. conventional node.js paradigm.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -