正在安装
在开始之前,请确保你已安装 Node.js 18 或更高版本。然后,为你的应用创建一个目录并进入该目录。
🌐 Before you begin, make sure you have Node.js 18 or higher installed. Then, create a directory for your application and navigate into it.
mkdir myappcd myapp使用 npm init 命令为你的应用创建一个 package.json 文件。有关 package.json 如何工作的更多信息,请参见 npm 的 package.json 处理细节。
🌐 Use the npm init command to create a package.json file for your application.
For more information on how package.json works, see Specifics of npm’s package.json handling.
npm inityarn initpnpm initbun init此命令会提示你输入多项内容,例如你的应用的名称和版本。 目前,你可以简单地按回车键以接受大多数默认设置,但以下情况除外:
🌐 This command prompts you for a number of things, such as the name and version of your application. For now, you can simply hit RETURN to accept the defaults for most of them, with the following exception:
entry point: (index.js)输入 app.js,或你希望主文件的名称。如果你希望它是 index.js,按回车键接受建议的默认文件名。
🌐 Enter app.js, or whatever you want the name of the main file to be. If you want it to be index.js, hit RETURN to accept the suggested default file name.
现在,在 myapp 目录中安装 Express,并将其保存到依赖列表中。例如:
🌐 Now, install Express in the myapp directory and save it in the dependencies list. For example:
npm install expressyarn add expresspnpm add expressbun add express要临时安装 Express 而不将其添加到依赖列表中:
🌐 To install Express temporarily and not add it to the dependencies list:
npm install express --no-savebun add express --no-saveTypeScript
Express 是用 JavaScript 编写的,并且不自带类型定义。要在 TypeScript 中使用它,需要将 TypeScript 与社区维护的 Express 和 Node.js 类型(来自 DefinitelyTyped)一起作为开发依赖安装:
🌐 Express is written in JavaScript and does not bundle its own type definitions. To use it with TypeScript, install TypeScript together with the community-maintained types for Express and Node.js (from DefinitelyTyped) as development dependencies:
npm install --save-dev typescript @types/express @types/nodeyarn add --dev typescript @types/express @types/nodepnpm add --save-dev typescript @types/express @types/nodebun add --dev typescript @types/express @types/nodeWarning
有些中间件不会打包自己的类型定义。如果你添加一个 TypeScript 报告为未定义类型的官方中间件包,也可以从 DefinitelyTyped 安装其类型作为开发依赖,例如在 cors 旁边安装 @types/cors。
🌐 Some middleware does not bundle its own type definitions. If you add an official middleware package
that TypeScript reports as untyped, also install its types from DefinitelyTyped as a dev dependency,
for example @types/cors alongside cors.
添加一个 tsconfig.json。这些选项与 Node.js 运行 TypeScript 的方式相同,并让编译器拒绝 Node 无法剥除的语法(例如 enum、命名空间和参数属性):
🌐 Add a tsconfig.json. These options mirror how Node.js runs TypeScript and make the compiler reject
non-erasable syntax (such as enums, namespaces, and parameter properties) that Node cannot strip:
{ "compilerOptions": { "target": "esnext", "module": "nodenext", "rewriteRelativeImportExtensions": true, "erasableSyntaxOnly": true, "verbatimModuleSyntax": true, "noEmit": true, "strict": true, "skipLibCheck": true }}用 TypeScript 编写你的应用,为请求和响应对象添加注解:
🌐 Write your application in TypeScript, annotating the request and response objects:
import express, { type Express, type Request, type Response } from 'express';
const app: Express = express();
app.get('/', (req: Request, res: Response) => { res.send('Hello World!');});
app.listen(3000);你不需要为所有内容添加注解。当你将一个处理函数直接传递给路由方法或app.use()时,Express 会推断出 req、res 和 next 的类型,并且它会根据路径推断路由参数,因此 req.params.id 在 app.get('/users/:id', ...) 中是 string。只有在 TypeScript 无法从上下文推断的情况下才添加显式类型:例如错误处理中间件,其 (err, req, res, next) 签名无法被推断,以及你单独定义且未直接与路由相关联的处理函数。在这些情况下,为参数添加注解或将整个函数类型标注为 RequestHandler 或 ErrorRequestHandler。
🌐 You do not need to annotate everything. When you pass a handler directly to a route method or to
app.use(), Express infers the types of req, res, and next, and it infers route parameters
from the path, so req.params.id is a string in app.get('/users/:id', ...). Add explicit types
only where TypeScript has no context to infer from: error-handling middleware, whose
(err, req, res, next) signature is not inferred, and handlers you define separately from the route.
In those cases, annotate the parameters or type the whole function as RequestHandler or
ErrorRequestHandler.
直接使用 Node.js 运行该文件,它会去掉 TypeScript 类型并运行结果,而无需构建步骤:
🌐 Run the file directly with Node.js, which strips the TypeScript types and runs the result without a build step:
node src/app.tsNote
直接运行 .ts 文件需要 Node.js >= 22.18.0(或在 v23 版本线中 >= 23.6.0)和 TypeScript >= 5.8。Node 会去掉类型,但不会进行类型检查,因此请运行 npx tsc 来对你的项目进行类型检查。有关更多详情,请参阅 Node.js 关于原生运行 TypeScript的指南。
🌐 Running .ts files directly requires Node.js >= 22.18.0 (or >= 23.6.0 on the v23 line) and
TypeScript >= 5.8. Node strips the types but does not type-check them, so run npx tsc to type-check
your project. For more details, see the Node.js guide on
running TypeScript natively.