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
in controller better use reference path declaration or external modules when referencing models? overlap using standard node.js require statement?
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
Post a Comment