用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/microwind/ai-skills --skill express-js命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
基于 SOC 职业分类
正在显示 SKILL.md
| name | Express.js服务 |
| description | 当开发Express.js应用时,分析路由设计,优化中间件配置,解决性能问题。验证API架构,设计RESTful服务,和最佳实践。 |
| license | MIT |
Express.js是Node.js最流行的Web框架。不当的Express应用设计会导致性能问题、安全漏洞和维护困难。在开发Express应用前需要仔细分析架构需求。
核心原则: 好的Express应用应该模块化、高性能、安全可靠。坏的Express应用会导致代码混乱、性能瓶颈和安全风险。
始终:
触发短语:
问题:
路由结构混乱,缺乏一致性
错误示例:
- 路由命名不规范
- 缺乏RESTful设计
- 参数验证缺失
- 错误处理不统一
解决方案:
1. 遵循RESTful设计原则
2. 统一路由命名规范
3. 实施参数验证中间件
4. 建立统一错误处理机制
问题:
中间件配置过多或顺序不当
错误示例:
- 不必要的中间件加载
- 中间件执行顺序错误
- 同步中间件阻塞
- 中间件依赖混乱
解决方案:
1. 只加载必要的中间件
2. 正确配置中间件顺序
3. 使用异步中间件
4. 清理中间件依赖关系
问题:
应用响应慢,并发能力差
错误示例:
- 同步阻塞操作
- 内存泄漏
- 数据库连接未复用
- 缺乏缓存机制
解决方案:
1. 使用异步操作避免阻塞
2. 实施内存监控和清理
3. 配置数据库连接池
4. 添加适当的缓存策略
const express = require('express');
const fs = require('fs');
const path = require('path');
const { performance } = require('perf_hooks');
class ExpressAppAnalyzer {
constructor(appPath) {
this.appPath = appPath;
this.issues = [];
this.metrics = {
routes: [],
middlewares: [],
controllers: [],
performance: {}
};
}
analyzeApplication() {
try {
// 分析路由结构
this.analyzeRoutes();
// 分析中间件配置
this.analyzeMiddlewares();
// 分析控制器代码
this.analyzeControllers();
// 性能分析
this.analyzePerformance();
// 生成分析报告
return this.generateReport();
} catch (error) {
{ : };
}
}
() {
{
routesPath = path.(., );
(!fs.(routesPath)) {
..({
: ,
: ,
: ,
:
});
;
}
routeFiles = fs.(routesPath).( file.());
( file routeFiles) {
filePath = path.(routesPath, file);
content = fs.(filePath, );
.(file, content);
}
} (error) {
..({
: ,
: ,
: ,
:
});
}
}
() {
routeInfo = {
: fileName,
: [],
: []
};
httpMethods = [, , , , ];
routePatterns = [];
( method httpMethods) {
regex = (, );
match;
((match = regex.(content)) !== ) {
routePatterns.({
: method.(),
: match[],
: content.(, match.).().
});
}
}
routeInfo. = routePatterns;
( route routePatterns) {
(!.(route.)) {
routeInfo..({
: ,
: ,
: ,
:
});
}
}
(content.() || content.() || content.()) {
(!content.() && !content.() && !content.()) {
routeInfo..({
: ,
: ,
: ,
:
});
}
}
...(routeInfo);
..(...routeInfo.);
}
() {
(path.() || path.() || path.() || path.()) {
;
}
pathParts = path.().( part && !part.());
( part pathParts) {
(part.() || part === || part === ) {
;
}
([, , ].(part)) {
;
}
;
}
;
}
() {
{
appFile = path.(., );
serverFile = path.(., );
mainFile = ;
(fs.(appFile)) {
mainFile = appFile;
} (fs.(serverFile)) {
mainFile = serverFile;
}
(!mainFile) {
..({
: ,
: ,
: ,
:
});
;
}
content = fs.(mainFile, );
.(content);
} (error) {
..({
: ,
: ,
: ,
:
});
}
}
() {
middlewareInfo = {
: ,
: [],
: []
};
commonMiddlewares = [
{ : , : , : },
{ : , : , : },
{ : , : , : },
{ : , : , : },
{ : , : , : }
];
( middleware commonMiddlewares) {
(!middleware..(content)) {
middlewareInfo..({
: middleware.,
: ,
: ,
:
});
} {
middlewareInfo..(middleware.);
}
}
(!content.() || !content.()) {
middlewareInfo..({
: ,
: ,
: ,
:
});
}
lines = content.();
middlewareLines = [];
lines.( {
(line.() && !line.()) {
middlewareLines.({
: index + ,
: line.()
});
}
});
.(middlewareLines, middlewareInfo);
...(middlewareInfo);
..(...middlewareInfo.);
}
() {
hasErrorHandling = ;
hasRoutesAfterError = ;
( middleware middlewareLines) {
(middleware..()) {
hasErrorHandling = ;
} (hasErrorHandling && (middleware..(./routes) || middleware..())) {
hasRoutesAfterError = ;
}
}
(hasRoutesAfterError) {
middlewareInfo..({
: ,
: ,
: ,
:
});
}
}
() {
{
controllersPath = path.(., );
(!fs.(controllersPath)) {
..({
: ,
: ,
: ,
:
});
;
}
controllerFiles = fs.(controllersPath).( file.());
( file controllerFiles) {
filePath = path.(controllersPath, file);
content = fs.(filePath, );
.(file, content);
}
} (error) {
..({
: ,
: ,
: ,
:
});
}
}
() {
controllerInfo = {
: fileName,
: [],
: []
};
functionPattern = ;
match;
((match = functionPattern.(content)) !== ) {
controllerInfo..({
: match[],
: content.(, match.).().
});
}
(content.() && content.()) {
} (controllerInfo.. > ) {
controllerInfo..({
: ,
: ,
: ,
:
});
}
(!content.() && !content.()) {
controllerInfo..({
: ,
: ,
: ,
:
});
}
(content.() && !content.() && !content.()) {
}
...(controllerInfo);
..(...controllerInfo.);
}
() {
{
packageJsonPath = path.(., );
(!fs.(packageJsonPath)) {
..({
: ,
: ,
: ,
:
});
;
}
packageJson = .(fs.(packageJsonPath, ));
.(packageJson);
.(packageJson);
} (error) {
..({
: ,
: ,
: ,
:
});
}
}
() {
dependencies = packageJson. || {};
devDependencies = packageJson. || {};
expressVersion = dependencies.;
(expressVersion) {
(expressVersion.() || expressVersion.()) {
..({
: ,
: ,
: ,
:
});
}
}
securityDeps = [, , , ];
( dep securityDeps) {
(!dependencies[dep] && !devDependencies[dep]) {
..({
: ,
: ,
: ,
:
});
}
}
}
() {
dependencies = packageJson. || {};
devDependencies = packageJson. || {};
performanceDeps = [, , , ];
( dep performanceDeps) {
(!dependencies[dep] && !devDependencies[dep]) {
..({
: ,
: ,
: ,
:
});
}
}
}
() {
summary = {
: ..,
: ..( i. === ).,
: ..( i. === ).,
: ..( i. === ).,
: ..( i. === ).
};
recommendations = .();
{
summary,
: .,
: .,
recommendations,
: .(summary)
};
}
() {
recommendations = [];
issueTypes = {};
..( {
issueTypes[issue.] = (issueTypes[issue.] || ) + ;
});
(issueTypes. > ) {
recommendations.({
: ,
: ,
: ,
:
});
}
(issueTypes. > ) {
recommendations.({
: ,
: ,
: ,
:
});
}
(issueTypes. > ) {
recommendations.({
: ,
: ,
: ,
:
});
}
recommendations;
}
() {
score = ;
score -= summary. * ;
score -= summary. * ;
score -= summary. * ;
score -= summary. * ;
.(, score);
}
}
{
() {
. = appPath;
}
() {
optimizations = [];
packageOptimization = .();
(packageOptimization) {
optimizations.(packageOptimization);
}
middlewareOptimization = .();
(middlewareOptimization) {
optimizations.(middlewareOptimization);
}
routeOptimization = .();
(routeOptimization) {
optimizations.(routeOptimization);
}
{
optimizations,
: {
: optimizations.,
: .(optimizations)
}
};
}
() {
{
packageJsonPath = path.(., );
(!fs.(packageJsonPath)) {
{
: ,
: ,
:
};
}
packageJson = .(fs.(packageJsonPath, ));
optimizations = [];
performanceDeps = [, , ];
( dep performanceDeps) {
(!packageJson.[dep] && !packageJson.[dep]) {
optimizations.(dep);
}
}
(optimizations. > ) {
{
: ,
: ,
:
};
}
} (error) {
{
: ,
: ,
:
};
}
;
}
() {
{
appFile = path.(., );
serverFile = path.(., );
mainFile = ;
(fs.(appFile)) {
mainFile = appFile;
} (fs.(serverFile)) {
mainFile = serverFile;
}
(!mainFile) {
{
: ,
: ,
:
};
}
content = fs.(mainFile, );
optimizations = [];
(!content.()) {
optimizations.();
}
(!content.()) {
optimizations.();
}
(optimizations. > ) {
{
: ,
: ,
:
};
}
} (error) {
{
: ,
: ,
:
};
}
;
}
() {
{
routesPath = path.(., );
(!fs.(routesPath)) {
{
: ,
: ,
:
};
}
routeFiles = fs.(routesPath).( file.());
(routeFiles. === ) {
{
: ,
: ,
:
};
}
( file routeFiles) {
filePath = path.(routesPath, file);
stats = fs.(filePath);
(stats. > ) {
{
: ,
: ,
:
};
}
}
} (error) {
{
: ,
: ,
:
};
}
;
}
() {
improvements = {
: ,
: ,
:
};
optimizations.( {
(opt.) {
:
improvements. += ;
;
:
improvements. += ;
improvements. += ;
;
:
improvements. += ;
;
}
});
improvements;
}
}
() {
analyzer = ();
report = analyzer.();
.();
.();
.();
.();
.();
report..( {
.();
});
optimizer = ();
optimization = optimizer.();
.();
optimization..( {
.();
});
}
. = { , };
const express = require('express');
const { performance } = require('perf_hooks');
class ExpressPerformanceMonitor {
constructor(app) {
this.app = app;
this.metrics = {
requests: [],
responseTimes: [],
errorCounts: {},
routeStats: {}
};
this.setupMonitoring();
}
setupMonitoring() {
// 请求计时中间件
this.app.use((req, res, next) => {
const start = performance.now();
res.on('finish', () => {
const duration = performance.now() - start;
this.recordRequest(req, res, duration);
});
next();
});
// 错误监控中间件
this.app.use((err, req, res, next) => {
this.recordError(err, req);
(err);
});
}
() {
requestInfo = {
: req.,
: req.,
: res.,
: duration,
: ().(),
: req.(),
: req. || req..
};
...(requestInfo);
...(duration);
routeKey = ;
(!..[routeKey]) {
..[routeKey] = {
: ,
: ,
: ,
:
};
}
stats = ..[routeKey];
stats.++;
stats. += duration;
stats. = stats. / stats.;
(res. >= ) {
stats.++;
}
(... > ) {
...();
}
(... > ) {
...();
}
}
() {
errorKey = err. || ;
..[errorKey] = (..[errorKey] || ) + ;
}
() {
responseTimeStats = .();
errorRate = .();
topRoutes = .();
{
: ...,
responseTimeStats,
errorRate,
: ..,
topRoutes,
: ().()
};
}
() {
times = ..;
(times. === ) {
{ : , : , : , : , : };
}
sorted = times.( a - b);
len = sorted.;
{
: times.( sum + time, ) / len,
: sorted[],
: sorted[len - ],
: sorted[.(len * )],
: sorted[.(len * )]
};
}
() {
totalRequests = ...;
errorRequests = ...( req. >= ).;
totalRequests > ? (errorRequests / totalRequests) * : ;
}
() {
routes = .(..);
routes
.( b. - a.)
.(, )
.( ({ route, ...stats }));
}
}
. = ;