【node-express】在commonjs的项目中使用esm和ts开发的sdk

在commonjs的项目中使用esm和ts开发的sdk

效果

在一些demo中, 大部分代码是commonjs规范开发的,但是要用到的sdk是ts开发的并且仅支持esm,
又不想配置很复杂的工程项目,可以这么做。如果你有更好的建议,希望能得到你的指点。
源码:https://github.com/thinkasany/nestjs-course-code/tree/master/demo/cjsAndEsm
在这里插入图片描述

实现步骤

创建tsconfig.json文件

{
    "compilerOptions": {
      "target": "es6",
      "module": "commonjs",
      "strict": true,
      "esModuleInterop": true,
      "skipLibCheck": true,
      "forceConsistentCasingInFileNames": true,
      "moduleResolution": "node",
      "outDir": "./dist"  // 输出目录
    },
    "include": ["service/*.ts"],
    "exclude": ["node_modules"]
  }
  
 "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "npx tsc && nodemon main.js"
  },
const Client = require('../dist/address'); 

module.exports = function (app) {
  app.get('/health',  (req, res) => {
    return res.json({ message: 'OK', data: `Time Is ${new Date().toString()}` });
  });

  app.get('/address', async (req, res) => {
    const text = req.query.text; // 从请求参数中获取需要处理的文本
    try {
      const data = await Client.main(text);
      console.log('data', data);
      res.status(200).json({ message: 'Successfully processed the request.', data });
    } catch (error) {
      // 处理错误
      res.status(500).json({ error: error.message });
    }
  });
};

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值