Node.js安装Express,Node.js支持Typescript以及Express支持Typescript的步骤

1. Node.js 安装Express

    运行如下命令:

$ mkdir express-demo
$ cd express-demo

$ npm install express 
$ npm install body-parser //(可选)中间件,用于处理 JSON, Raw, Text 和 URL 编码的数据
$ npm install cookie-parser //(可选)通过req.cookies可以取到传过来的cookie,并把它们转成对象
$ npm install multer //(可选)中间件,用于处理 enctype="multipart/form-data"(设置表单的MIME编码)的表单数据
$ npm list express

创建一个server.js

var express = require('express');
var app = express();
 
app.listen(8081, function () {
  console.log("server is running on port 8081")
})

运行:

$ C:\Users\MyStudy\express-demo> node server.js
server is running on port 8081  //输出结果,浏览器访问http://localhost:8081

2. Node.js 支持Typescript

   运行如下命令:

$ mkdir tsc-node-demo
$ cd tsc-node-demo

$ npm init -y //初始化,创建package.json

$ npm install typescript --save-dev    //安装typescipt
$ npm install @types/node --save-dev   //是一个用于 TypeScript 的类型定义包,包含了 Node.js 的类型定义。这些类型定义允许 TypeScript 更好地理解 Node.js 的 API,从而提供类型检查和智能提示等功能

$ npx tsc --init //创建tsconfig.json

创建ts文件

const greeting: string = 'Hello, TypeScript with Node.js!';
console.log(greeting);

运行:

$ tsc index.ts  //如果报错:tsc : 无法加载文件 \AppData\Roaming\npm\tsc.ps1,因为在此系统上禁止运行脚本,请使用Git Bash
$ node index.js
Hello, TypeScript with Node.js! //输出结果

另外,还可以修改为使用npm运行

打开package.json,找到"scripts"(没有就新建),修改或者添加build,start命令,如下:

"scripts": {
    "build": "tsc",
    "start": "node index.js"
  }

然后就可以通过下面命令运行:

$ npm run build
$ npm start

3. Express支持Typescript

   依次运行如下命令:

$ mkdir tsc-express-demo
$ cd tsc-express-demo

$ npm init -y
$ npm install express
$ npm install typescript @types/express @types/node --save-dev

$ npx tsc --init

打开tsconfig.json,修改/添加,但不局限于以下配置 (根据你实际项目来):

{
  "compilerOptions": {
    "esModuleInterop": true,
    "target": "es2016",                                  /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
    "module": "commonjs",                                /* Specify what module code is generated. */
    "outDir": "./dist",                                   /* Specify an output folder for all emitted files. */
    "strict": true,                                      /* Enable all strict type-checking options. */
    "skipLibCheck": true                                 /* Skip type checking all .d.ts files. */
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "**/__tests__"]
}

新建 src/index.ts (因为我们在tsconfig.json里配置的路径是 src/**/*)

import express, { Request, Response } from 'express';

const app = express();
const port = 3000;

app.get("/", (req: Request, res: Response) => {
  res.send("Hello, TypeScript with Express!");
});

app.listen(port, () => {
  console.log("server is running on port " + port);
});

运行:

$ npx tsc
$ node dist/index.js
server is running on port 3000 //输出结果.浏览器用http://localhost:3000访问

另外,还可以修改为使用npm运行

打开package.json,修改或添加"scripts"

 "scripts": {
    "build": "tsc",
    "start": "node dist/index.js",
    "dev": "ts-node src/index.ts"
  }

 然后就可以通过下面命令运行:

//启动dev开发环境,不需要生成js文件
$ npm run dev
//或者
//生成js文件,可以用于发布
$ npm run build //生成js文件
$ npm start

如果我们想自动监视项目中的应用程序文件的更改,并在更改时自动重启服务器,可以安装nodemon

npm install nodemon --save-dev

打开package.json,修改start,dev命令,以整合typescript和nodemon:

 "scripts": {
    "build": "tsc",
    "start": "nodemon dist/index.js",
    "dev": "nodemon --exec ts-node src/index.ts"
  },

还可以创建一个nodemon.json 配置文件来定义nodemon 的行为。例如:

{
  "watch": ["src"], //监视 src 文件夹的更改
  "ext": "ts,js",   //关注文件扩展名为 .ts 和 .js 的文件
  "exec": "ts-node src/index.ts" //使用 ts-node 来执行 .ts 文件
}

运行命令不变,会输出如下内容:

[nodemon] 3.1.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,cjs,json
[nodemon] starting `node dist/index.js`
server is running on port 3000

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值