后端最佳实践项目教程

后端最佳实践项目教程

backend-best-practicesBest practices, tools and guidelines for backend development. Code examples in TypeScript + NodeJS项目地址:https://gitcode.com/gh_mirrors/bac/backend-best-practices

1. 项目的目录结构及介绍

backend-best-practices/
├── .github
│   └── workflows
├── docs
├── src
│   ├── api
│   ├── application
│   ├── domain
│   ├── infrastructure
│   └── shared
├── .gitignore
├── .prettierrc
├── .eslintrc.json
├── package.json
├── tsconfig.json
└── README.md
  • .github/workflows: 存放GitHub Actions的工作流配置文件。
  • docs: 存放项目文档。
  • src: 源代码目录。
    • api: API层代码。
    • application: 应用层代码。
    • domain: 领域层代码。
    • infrastructure: 基础设施层代码。
    • shared: 共享代码。
  • .gitignore: Git忽略文件配置。
  • .prettierrc: Prettier代码格式化配置。
  • .eslintrc.json: ESLint代码检查配置。
  • package.json: 项目依赖和脚本配置。
  • tsconfig.json: TypeScript配置。
  • README.md: 项目说明文档。

2. 项目的启动文件介绍

项目的启动文件通常位于src目录下,例如src/index.tssrc/server.ts。这个文件负责启动服务器和初始化应用程序。

// src/index.ts
import express from 'express';
import { setupRoutes } from './api/routes';

const app = express();
const port = process.env.PORT || 3000;

setupRoutes(app);

app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

3. 项目的配置文件介绍

  • .prettierrc: 配置Prettier代码格式化规则。
{
  "singleQuote": true,
  "trailingComma": "all",
  "printWidth": 80
}
  • .eslintrc.json: 配置ESLint代码检查规则。
{
  "parser": "@typescript-eslint/parser",
  "plugins": ["@typescript-eslint"],
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/recommended"
  ],
  "rules": {
    "no-console": "warn"
  }
}
  • package.json: 配置项目依赖和脚本。
{
  "name": "backend-best-practices",
  "version": "1.0.0",
  "scripts": {
    "start": "ts-node src/index.ts",
    "build": "tsc",
    "lint": "eslint src --ext .ts"
  },
  "dependencies": {
    "express": "^4.17.1"
  },
  "devDependencies": {
    "@types/express": "^4.17.13",
    "ts-node": "^10.4.0",
    "typescript": "^4.5.2"
  }
}
  • tsconfig.json: 配置TypeScript编译选项。
{
  "compilerOptions": {
    "target": "ES6",
    "module": "commonjs",
    "outDir": "./dist",
    "strict": true,
    "esModuleInterop": true
  },
  "include": ["src"]
}

backend-best-practicesBest practices, tools and guidelines for backend development. Code examples in TypeScript + NodeJS项目地址:https://gitcode.com/gh_mirrors/bac/backend-best-practices

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尚学红Vandal

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值