初始化一个node项目

npm init初始化一个typescript的express项目,安装几个包,然后npm run dev

首先进入项目文件夹,执行npm init,会生成package.json文件

1
npm init

安装开发依赖typescript

1
npm install typescript --save-dev

package.json文件添加脚本

1
2
3
4
5
6
7
{
// ..
"scripts": {
"tsc": "tsc"
},
// ..
}

执行init初始化ts配置

1
npm run tsc -- --init

安装express eslint

1
2
npm install express
npm install --save-dev eslint @types/express @typescript-eslint/eslint-plugin @typescript-eslint/parser

package.json文件添加脚本

1
2
3
4
5
6
7
8
9
{
// ..
"scripts": {
"tsc": "tsc",
"lint": "eslint --ext .ts .",
"start": "node build/index.js"
},
// ..
}

安装热重载ts-node-dev

1
npm install --save-dev ts-node-dev

package.json文件添加脚本 dev

1
2
3
4
5
6
7
8
9
10
{
// ..
"scripts": {
"tsc": "tsc",
"dev": "ts-node-dev index.ts",
"lint": "eslint --ext .ts .",
"start": "node build/index.js"
},
// ..
}

添加文件index.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import express from 'express';
const app = express();
app.use(express.json());

const PORT = 3000;

app.get('/ping', (_req, res) => {
console.log('someone pinged here');
res.send('pong');
});

app.listen(PORT, () => {
console.log(`Server running on port ${PORT} http://localhost:${PORT}/ping`);
});

运行npm run dev,浏览器打开 http://localhost:3000/ping 返回pong就成功了

添加别的库

跨域

1
2
npm install cors
npm install --save-dev @types/cors

index.ts

1
2
3
4
5
6
7
8
import express from 'express';
import cors from 'cors';

const app = express();
app.use(express.json());
app.use(cors());

// ..

uuid

1
2
npm install uuid
npm install --save-dev @types/uuid

xxxService.ts

1
2
3
import { v1 as uuid } from 'uuid';

const id: string = uuid();

配置文件

tsconfig.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig to read more about this file */
"target": "ES6", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"module": "commonjs", /* Specify what module code is generated. */
"resolveJsonModule": true, /* Enable importing .json files. */
"outDir": "./build/", /* Specify an output folder for all emitted files. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
"strict": true, /* Enable all strict type-checking options. */
"noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
"noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
"noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
"noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
}
}

.eslintrc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking"
],
"plugins": ["@typescript-eslint"],
"env": {
"browser": true,
"es6": true,
"node": true
},
"rules": {
"@typescript-eslint/semi": ["error"],
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/restrict-template-expressions": "off",
"@typescript-eslint/restrict-plus-operands": "off",
"@typescript-eslint/no-unsafe-member-access": "off",
"@typescript-eslint/no-unused-vars": [
"error",
{ "argsIgnorePattern": "^_" }
],
"no-case-declarations": "off"
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.json"
}
}

.eslintignore

1
2
build/
dist/

.gitignore

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
.history/
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

作者

Wei Mo

发布于

2024-03-05

更新于

2024-03-21

许可协议

评论