Node 系列 - 002 - TypeScript

——————————☆☆☆——————————

Node 系列相应地址:

  • 代码仓库:https://github.com/LiangJunrong/all-for-one

  • 文章仓库:https://github.com/LiangJunrong/document-library/tree/master/%E7%B3%BB%E5%88%97-%E5%89%8D%E7%AB%AF%E8%B5%84%E6%96%99/Node

——————————☆☆☆——————————

TypeScript 是 JavaScript 的超集,为语言增加了新的功能(下面简称 TS)。

jsliang 羡慕 TypeScript 很久了,一直没有自己去搭建过,都是用别人搭建好的,恰好这次要尝试,那就折腾个痛快。

这篇文章通过配置 Node.js 集成 TS,来快速讲解 TS 的使用。

一 目录

不折腾的前端,和咸鱼有什么区别

目录
一 目录
二 Node.js 快速集成 TS
 2.1 目录结构
 2.2 初始化步骤
三 tsconfig.json 讲解
 3.1 compilerOptions 可配置项
 3.2 files 可配置项
 3.3 include 和 exclude 可配置项
四 ESLint
五 总结
六 参考文献

二 Node.js 快速集成 TS

2.1 目录结构

在这之前,我们先明白即将构建的目录:

util
 - src
  - index.ts
 - tsconfig.json
 - package.json

util 就是仓库名称(文件夹名称),可以随意换个其他文件夹

除了 index.ts 是人工添加的,其他文件均有命令行生成,可以不理会

那么,Here We go~

2.2 初始化步骤

首先,初始化 package.json

  • npm init --yes

如果仓库名为中文名,需要 npm init 逐项填写

然后,如果在 index.ts 中,编写了以下代码:

index.ts

const path = require('path');

console.log(path);

此时执行 node src/index.ts,会看到报错:

internal/modules/cjs/loader.js:883
  throw err;
  ^

Error: Cannot find module 'F:\jsliang\index.ts'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
    at Function.Module._load (internal/modules/cjs/loader.js:725:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)   
    at internal/main/run_main_module.js:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

也有可能不报错!

如果你使用的是 VS Code 开发软件,会看到提示:

找不到名称 "require"。是否需要为节点安装类型定义? 请尝试使用 `npm i --save-dev @types/node`。ts(2580)

意思就是 pathrequire 模块都是 Node.js 的东西,使用它需要安装 Node.js 的声明文件,即安装 @types/node 这个包。

接着,如果单单安装 @types/node,是还不够的,因为 @types/node 仅仅是 TS

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Node-s7 是一个用于连接和通信 Siemens S7 系列 PLC 的 Node.js 模块。在 NestJS 中使用 node-s7,你需要安装该模块并在你的代码中引入它。 以下是一个示例: 1. 安装 node-s7 模块 ```bash npm install node-s7 --save ``` 2. 在你的 NestJS 服务中引入 node-s7 ```typescript import { Injectable } from '@nestjs/common'; import { S7Client } from 'node-s7'; @Injectable() export class S7Service { private readonly client: S7Client; constructor() { this.client = new S7Client(); } async connect() { await this.client.ConnectTo( '192.168.1.10', // PLC IP 地址 0, // PLC 端口 1 // PLC 编号 ); } async readData() { const data = await this.client.DBRead(1, 0, 10); // 从 DB1 中读取 10 个字节数据 return data; } async writeData() { const data = Buffer.from([0x01, 0x02, 0x03, 0x04]); await this.client.DBWrite(1, 0, data); // 将数据写入 DB1 } async disconnect() { await this.client.Disconnect(); } } ``` 在上面的示例中,我们创建了一个 `S7Service` 类,该类包含了连接、读取和写入数据到 PLC 的方法。我们通过 `node-s7` 模块中的 `S7Client` 类来连接和通信 PLC。在 `connect` 方法中,我们连接到了 PLC,并在 `readData` 和 `writeData` 方法中读取和写入数据到 PLC。在 `disconnect` 方法中,我们断开了与 PLC 的连接。 你可以在控制器中使用 `S7Service` 类来读取和写入数据到 PLC。 ```typescript import { Controller, Get, Post } from '@nestjs/common'; import { S7Service } from './s7.service'; @Controller('plc') export class S7Controller { constructor(private readonly s7Service: S7Service) {} @Get('connect') async connect() { await this.s7Service.connect(); return { message: 'Connected to PLC' }; } @Get('read') async readData() { const data = await this.s7Service.readData(); return { data }; } @Post('write') async writeData() { await this.s7Service.writeData(); return { message: 'Data written to PLC' }; } @Get('disconnect') async disconnect() { await this.s7Service.disconnect(); return { message: 'Disconnected from PLC' }; } } ``` 在上面的示例中,我们创建了一个 `S7Controller` 控制器,并注入了 `S7Service` 服务。我们使用 `@Get` 和 `@Post` 装饰器来定义了连接、读取和写入数据、断开连接的路由。在每个路由方法中,我们调用了 `S7Service` 中对应的方法。 这就是在 NestJS 中使用 node-s7 的基本示例。你可以根据自己的需求进行修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值