TypeScript-Node-Starter 项目教程
2024-08-23 20:15:49作者:仰钰奇
1. 项目的目录结构及介绍
TypeScript-Node-Starter/
├── src/
│ ├── controllers/
│ ├── models/
│ ├── routes/
│ ├── services/
│ ├── types/
│ ├── app.ts
│ └── server.ts
├── config/
│ ├── custom-environment-variables.json
│ ├── default.json
│ └── production.json
├── public/
│ ├── css/
│ ├── js/
│ └── images/
├── views/
│ └── index.pug
├── .env
├── .gitignore
├── package.json
├── tsconfig.json
└── README.md
目录结构介绍
- src/: 包含所有TypeScript源代码文件。
- controllers/: 处理HTTP请求的控制器。
- models/: 数据库模型定义。
- routes/: 路由定义。
- services/: 业务逻辑服务。
- types/: TypeScript类型定义。
- app.ts: 应用程序的入口文件,配置中间件和路由。
- server.ts: 启动服务器文件。
- config/: 配置文件目录。
- custom-environment-variables.json: 环境变量映射。
- default.json: 默认配置。
- production.json: 生产环境配置。
- public/: 静态资源文件目录。
- css/: CSS文件。
- js/: JavaScript文件。
- images/: 图片文件。
- views/: 视图模板文件目录。
- index.pug: 主页模板。
- .env: 环境变量配置文件。
- .gitignore: Git忽略文件配置。
- package.json: 项目依赖和脚本配置。
- tsconfig.json: TypeScript编译配置。
- README.md: 项目说明文档。
2. 项目的启动文件介绍
server.ts
server.ts 是项目的启动文件,负责启动Express服务器并监听指定端口。
import app from './app';
import http from 'http';
import { AddressInfo } from 'net';
const server = http.createServer(app);
const port = process.env.PORT || '3000';
app.set('port', port);
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
function onError(error: NodeJS.ErrnoException): void {
if (error.syscall !== 'listen') {
throw error;
}
const bind = typeof port === 'string' ? `Pipe ${port}` : `Port ${port}`;
switch (error.code) {
case 'EACCES':
console.error(`${bind} requires elevated privileges`);
process.exit(1);
break;
case 'EADDRINUSE':
console.error(`${bind} is already in use`);
process.exit(1);
break;
default:
throw error;
}
}
function onListening(): void {
const addr = server.address() as AddressInfo;
const bind = typeof addr === 'string' ? `pipe ${addr}` : `port ${addr.port}`;
console.log(`Listening on ${bind}`);
}
app.ts
app.ts 是应用程序的入口文件,负责配置中间件和路由。
import express from 'express';
import path from 'path';
import logger from 'morgan';
import cookieParser from 'cookie-parser';
import bodyParser from 'body-parser';
import dotenv from 'dotenv';
import mongoose from 'mongoose';
import index from './routes/index';
import users from './routes/users';
dotenv.config();
const app = express();
// 连接数据库
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/test');
// 视图引擎设置
app.set('views', path.join(__dirname, 'views'));
app.set('view engine',
登录后查看全文
热门项目推荐
相关项目推荐
atomcodeClaude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed. Get StartedRust0282
GLM-5.2智谱开源 GLM-5.2,这是针对长文本任务的最新旗舰模型。相较于前代产品 GLM-5.1,它在长文本任务处理能力上实现了显著飞跃,并且首次在稳定的 100 万 token 上下文中提供这一能力。Jinja00
JoyAI-VL-Interaction-Preview京东开源首个开源、视觉驱动的实时交互模型——它能实时监控视频流,并自主决定何时发言、保持沉默或委托任务。Jinja00
cann-learning-hubCANN 学习中心仓,支持在线互动运行、边学边练,提供教程、示例与优化方案,一站式助力昇腾开发者快速上手。Jupyter Notebook0190
MaxKB强大易用的开源企业级智能体平台Python02
note-gen一款跨平台的 Markdown AI 笔记软件,致力于使用 AI 建立记录和写作的桥梁。TSX011
热门内容推荐
最新内容推荐
项目优选
收起
暂无描述
Dockerfile
789
5.18 K
本项目是CANN提供的transformer类大模型算子库,实现网络在NPU上加速计算。
C++
902
2.1 K
本项目是CANN提供的神经网络类计算算子库,实现网络在NPU上加速计算。
C++
727
1.45 K
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
473
484
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
1.14 K
1.18 K
deepin linux kernel
C
32
16
Ascend Extension for PyTorch
Python
769
998
JiuwenSwarm 是一款基于openJiuwen开发的智能AI Agent,它能够将大语言模型的强大能力,通过你日常使用的各类通讯应用,直接延伸至你的指尖。
Python
2.53 K
693
Claude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed.
Get Started
Rust
2.56 K
282
CANNBot 是面向 CANN 开发的用于提升开发效率的系列智能体,本仓库为其提供可复用的 Skills 模块。
Python
1.09 K
687