一个具有代码模板生成能力的全栈开发框架,帮助开发者高效构建Web应用。
开源情况
NSGM CLI 是一个活跃维护的开源项目,欢迎社区贡献和使用:
- NPM包:https://www.npmjs.com/package/nsgm-cli
- GitHub仓库:https://github.com/erishen/nsgm
- 最新版本:
- 下载量:
- 开源协议:Apache License 2.0 (http://www.apache.org/licenses/)
快速安装
# 全局安装
npm install -g nsgm-cli
# 或作为项目依赖安装
npm install --save nsgm-cli
为什么选择NSGM CLI?
- 开箱即用:集成了现代Web开发所需的所有核心功能
- 开发效率:通过代码模板生成大幅提高开发速度
- 全栈解决方案:前后端一体化设计,无缝衔接
- 活跃维护:定期更新和bug修复,确保框架稳定性
- 社区支持:开源社区持续贡献和改进
技术栈
- Next.js – React框架
- Styled-components – CSS-in-JS解决方案
- GraphQL – API查询语言
- MySQL – 关系型数据库
特性
- 全栈架构设计
- 自动代码模板生成
- 快速开发工作流
- 集成GraphQL API
- MySQL数据库支持
- 使用bcrypt加密的安全登录系统
- 完整的项目脚手架
- 开发与生产环境配置
- 自动化部署支持
适用场景
- 企业级Web应用开发
- 个人项目快速原型设计
- 全栈开发学习与教学
- 需要高效开发流程的团队
- 需要统一技术栈的中大型项目
命令行工具
基本命令
命令 | 描述 |
---|---|
nsgm init |
初始化项目 |
nsgm upgrade |
升级项目基础文件 |
nsgm create |
创建模板页面 |
nsgm delete |
删除模板页面 |
nsgm deletedb |
删除模板页面和数据库表 |
nsgm dev |
开发模式 |
nsgm start |
生产模式 |
nsgm build |
构建项目 |
nsgm export |
导出静态页面 |
参数说明
-
dictionary: 与
export
/init
命令一起使用,默认值为webapp
nsgm init dictionary=webapp # 或简化为 nsgm init webapp
-
controller: 与
create
/delete
命令一起使用,必需参数nsgm create math
-
action: 与
create
/delete
命令一起使用,默认值为manage
,跟在controller后面nsgm create math test
开发流程示例
1. 创建新项目
# 安装CLI工具
npm install -g nsgm-cli
# 初始化新项目
nsgm init myproject
# 进入项目目录
cd myproject
# 安装依赖
npm install
2. 创建功能模块
# 创建用户管理模块
nsgm create user
# 创建带自定义操作的产品模块
nsgm create product detail
3. 启动开发服务器
# 启动开发模式
nsgm dev
快速设置
-
生成密码哈希:
# 使用npm脚本 npm run generate-password yourSecurePassword
-
创建
.env
文件:LOGIN_USERNAME=admin LOGIN_PASSWORD_HASH=your_generated_hash_here
-
确保
.env
在你的.gitignore
文件中。
⚠️ 重要提示: 永远不要将密码或.env
文件提交到版本控制系统。
项目配置
next.config.js
const { nextConfig } = require('nsgm-cli')
const projectConfig = require('./project.config')
const { version, prefix, protocol, host } = projectConfig
module.exports = (phase, defaultConfig) => {
let configObj = nextConfig(phase, defaultConfig, {
version,
prefix,
protocol,
host
})
return configObj
}
mysql.config.js
const { mysqlConfig } = require('nsgm-cli')
const { mysqlOptions } = mysqlConfig
const { user, password, host, port, database } = mysqlOptions
module.exports = {
mysqlOptions: {
user,
password,
host,
port,
database
}
}
project.config.js
const { projectConfig } = require('nsgm-cli')
const pkg = require('./package.json')
const { prefix, protocol, host, port } = projectConfig
const { version } = pkg
module.exports = {
version,
prefix,
protocol,
host,
port
}
服务器目录结构
项目根目录中的server
文件夹包含以下内容:
目录说明
apis/
– 存储REST API接口modules/
– 存储GraphQL解析器和模式plugins/
– 存储GraphQL插件*.js
– 路由文件
示例代码
路由文件示例 (server/rest.js)
const express = require('express')
const moment = require('moment')
const router = express.Router()
router.use((req, res, next) => {
const fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl
console.log(moment().format('YYYY-MM-DD HH:mm:ss') + ' ' + fullUrl)
next()
})
router.get('/*', (req, res) => {
res.statusCode = 200
res.json({ name: 'TEST' })
})
module.exports = router
REST API示例 (server/apis/hello.js)
const express = require('express')
const router = express.Router()
router.get('/*', (req, res) => {
res.statusCode = 200
res.json({ name: 'Hello' })
})
module.exports = router
GraphQL模式示例 (server/modules/link/schema.js)
module.exports = {
query: `
link: String
`,
mutation: `
linkUpdate(link: Date): String
`,
subscription: ``,
type: ``
}
GraphQL解析器示例 (server/modules/link/resolver.js)
let localLink = ''
module.exports = {
link: () => {
return localLink
},
linkUpdate: ({ link }) => {
console.log('link', link)
localLink = link
return localLink
}
}
GraphQL插件示例 (server/plugins/date.js)
const moment = require('moment')
const { Kind } = require('graphql/language')
const { GraphQLScalarType } = require('graphql')
const customScalarDate = new GraphQLScalarType({
name: 'Date',
description: 'Date custom scalar type',
parseValue: (value) => moment(value).valueOf(),
serialize: (value) => moment(value).format('YYYY-MM-DD HH:mm:ss:SSS'),
parseLiteral: (ast) => (ast.kind === Kind.INT ? parseInt(ast.value, 10) : null)
})
module.exports = { Date: customScalarDate }
社区与支持
- 问题反馈:如果您在使用过程中遇到任何问题,请在GitHub Issues提交
- 功能请求:有新功能建议?欢迎在Issues中提出
- 贡献代码:我们欢迎Pull Requests,请确保遵循项目的代码规范
- 联系作者:有任何问题可以通过GitHub联系项目维护者
成功案例
NSGM CLI已被用于多个成功的Web应用项目:
- 企业内部管理系统
- 数据可视化平台
- 电子商务网站
- 个人博客系统
- 教育培训平台
未来规划
我们正在积极开发以下功能:
- 更多数据库支持(MongoDB, PostgreSQL)
- 容器化部署支持
- 更丰富的UI组件库
- 微服务架构支持
- 更完善的文档和教程
NSGM CLI – 让全栈开发更简单、更高效