最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can I dynamically import TypeScript modules in Node.jsExpress? - Stack Overflow

programmeradmin0浏览0评论

I have a Node.js server built with Express.js and coded in TypeScript. Here's a snippet of the get call for my server:

server.ts

private get(req: Request, res: Response, next: NextFunction, objectName: string) {
    var DatabaseObject = require("./models/" + objectName + ".js")(this.orm, Sequelize.DataTypes);
    var Transform = require("./routes/" + objectName + ".js");
    var transform = new Transform();

    // ...

    console.log(req.query["columns"]);
    console.log(transform.columnWhitelist);
    console.log(transform);

    // ...

    if (transform.columnWhitelist) {
        console.log("Column Whitelist Exists.");
    }
    // ...
}

It dynamically loads the Sequelize module for the database object being requested in the URL and then tries to load a TypeScript module with rules on what columns can be selected, which columns can be queried on, etc. Here's the beginning of my ruleset class:

account.ts

export default class Transform {
    static columnWhitelist : Object = {"id": "id", "name": "name", "parentAccountId":"parentAccountId", "masterAccountId":"masterAccountId"};

    constructor() { }
}

However, running my application, I get:

id,name,parentAccountId
undefined
{ default: 
   { [Function: Transform]
     columnWhitelist: 
      { id: 'id',
        name: 'name',
        parentAccountId: 'parentAccountId',
        masterAccountId: 'masterAccountId' } } }

Making the call transform.columnWhitelist, I get undefined, despite seeing it in the generated JavaScript file as well. I've also tried just:

    var transform = require("./routes/" + objectName + ".js");

Or:

    var transform = require("./routes/" + objectName + ".js")();

But neither of these work either.

I have a Node.js server built with Express.js and coded in TypeScript. Here's a snippet of the get call for my server:

server.ts

private get(req: Request, res: Response, next: NextFunction, objectName: string) {
    var DatabaseObject = require("./models/" + objectName + ".js")(this.orm, Sequelize.DataTypes);
    var Transform = require("./routes/" + objectName + ".js");
    var transform = new Transform();

    // ...

    console.log(req.query["columns"]);
    console.log(transform.columnWhitelist);
    console.log(transform);

    // ...

    if (transform.columnWhitelist) {
        console.log("Column Whitelist Exists.");
    }
    // ...
}

It dynamically loads the Sequelize module for the database object being requested in the URL and then tries to load a TypeScript module with rules on what columns can be selected, which columns can be queried on, etc. Here's the beginning of my ruleset class:

account.ts

export default class Transform {
    static columnWhitelist : Object = {"id": "id", "name": "name", "parentAccountId":"parentAccountId", "masterAccountId":"masterAccountId"};

    constructor() { }
}

However, running my application, I get:

id,name,parentAccountId
undefined
{ default: 
   { [Function: Transform]
     columnWhitelist: 
      { id: 'id',
        name: 'name',
        parentAccountId: 'parentAccountId',
        masterAccountId: 'masterAccountId' } } }

Making the call transform.columnWhitelist, I get undefined, despite seeing it in the generated JavaScript file as well. I've also tried just:

    var transform = require("./routes/" + objectName + ".js");

Or:

    var transform = require("./routes/" + objectName + ".js")();

But neither of these work either.

Share Improve this question edited Mar 29, 2017 at 18:31 NobleUplift asked Mar 27, 2017 at 20:44 NobleUpliftNobleUplift 6,0438 gold badges49 silver badges93 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

If you have monjs as a module in your tsconfig.json and Transform class exported as default you should probably import it as

 var transform = require("./routes/" + objectName + ".js").default;

If you do not want to use

var transform = require("./routes/" + objectName + ".js").default;

You can export your class by doing

class Transform {
     static columnWhitelist: Object = {"id": "id", "name": "name", "parentAccountId": "parentAccountId", "masterAccountId": "masterAccountId"};

     constructor () {}
}

export = Transform;

After you can do again as before:

var transform = require ("./ routes /" + objectName + ".js");
发布评论

评论列表(0)

  1. 暂无评论