Node.js 安装 Express、Node.js 支持 Typescript 和 Express 支持 Typescript 的步骤
最编程
2024-09-30 07:12:55
...
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
上一篇: 2024 前端技术开发概览
下一篇: 可视化大屏
推荐阅读
-
Node.js 安装 Express、Node.js 支持 Typescript 和 Express 支持 Typescript 的步骤
-
支持 typescript 的 Node.js 字幕解析器,支持 LRC 和 SRT
-
Adobe国际认证中文官方网站】Adobe中国摄影计划,免费安装正版激活--Adobe Creative Cloud中国摄影计划。与此同时,Adobe宣布天猫为Adobe Creative Cloud中国摄影计划的电商战略合作伙伴,并将与其合作上线Adobe天猫官方旗舰店。 此举无疑一方面扩大了Adobe在中国的影响力,另一方面也有助于国内用户更好地培养正版软件意识,推动Adobe软件在中国的正版化进程。 网络异常,图片无法显示 ||网络异常 Adobe Creative Cloud中国摄影计划包括Photoshop和Lightroom Classic两大桌面创意工具,以及iOS版Photoshop Express。 其中,Adobe Lightroom Classic和Adobe Photoshop作为两款常用的图像处理软件,对于那些玩摄影、后期修图的创意设计人群无疑有着巨大的帮助,而LR+PS套装对于摄影领域用户的重要性自不必说,正版产品的性能实时更新也可以放心!体验最新功能,对于新镜头(补偿)和机身(RAW 读取)都能第一时间适应。不信你看: Photoshop 图像合成 裁剪、移除对象、润饰合成照片、玩转色彩和特效,创建精美图片和艺术品! Lightroom Classic 照片编辑 轻松批量管理和编辑照片,内置专业创意控件和摄影师预设,让你的照片大放异彩。 手机 PS 便捷编辑 Photoshop Express 支持多种滤镜、贴纸,手机即可完成抠图、除雾等任务 人工智能编辑工具 神经滤镜、快速点击选区、自动选择主题等人工智能功能让图像编辑更轻松 创意画笔内容识别 定制艺术画笔工具,实现个性化效果;内容识别填充,智能去除无用物体。 Adobe Creative Cloud 中国摄影计划的推出,为中国的专业摄影师、摄影爱好者、后期修图和其他创意设计人员带来了全方位的内容和体验。 网络异常,图片无法显示 ||网络异常 当然,不可否认的是,"由于盗版软件缺乏开发、维护和升级成本,销售价格远低于正版软件。再加上很多普通人并不需要使用正版软件的复杂功能,版权观念较淡,还是有大量的创意设计人员会选择盗版软件"。 但事实上,当所有的软件都不再是单一的软件,而是变成一种服务时,单机版盗版的存在就逐渐成为鸡肋。因为有太多的服务让你即使是所谓的 "完美破解",也无法享受,Adobe Cloud 就是一个很好的例子,所谓的完美破解,你只能使用 "Adobe "的一半,对于更精彩的 "云",只能望云兴叹。更何况,越来越多的设计工具从免费走向付费,越来越多的设计师和企业已经接受了付费使用的模式。 其次,对于互联网时代的企业数字化转型而言,数字化合规至关重要。21年来,使用盗版PS和未经授权的方正字体被指侵权的事情闹得沸沸扬扬,虽然新闻真假难辨,但也给使用盗版工具的用户敲响了警钟。 付费使用正版工具,可以更放心地进行设计,不用担心版权风险!
-
CentOS 7.9部署Node.js LTS V18.16.0-说明:使用上述方式安装node-v16.20.0-linux-x64.tar.gz(下载地址https://nodejs.org/dist/),不会报错。 上述安装报错排查: (1)查看GLIBC支持版本: # strings /lib64/libc.so.6 | grep ^GLIBC_ 说明:不支持GLIBC_2.25和GLIBC_2.28。 # strings /lib64/libm.so.6 | grep ^GLIBC_ 说明:不支持GLIBC_2.27。 (2)查看CXXABI支持版本: # strings /lib64/libstdc++.so.6 | grep ^CXXABI_ 说明:不支持CXXABI_1.3.9。 (3)查看GLIBCXX支持版本: # strings /lib64/libstdc++.so.6 | grep ^GLIBCXX_ 说明:不支持GLIBCXX_3.4.20和GLIBCXX_3.4.21。 解决方案:需要升级系统中的make、GCC和GLIBC版本。