约翰卡马特
Happy holidays developers ? Recently, I published Matterhorn ?, an API boilerplate project built with Node.js and TypeScript. The API server uses Fastify, a fast and low overhead web framework. The project comes with a configured type system (TypeScript), test runner (Jest), linter (TSLint), and even a CI pipeline (Azure DevOps).
节日快乐的开发人员? 最近,我发布了M atterhorn?,这是一个用Node.js和TypeScript构建的API样板项目。 API服务器使用Fastify,这是一种快速,低开销的Web框架。 该项目带有配置的类型系统(TypeScript),测试运行器(Jest),linter(TSLint)甚至CI管道(Azure DevOps)。
This article will give a brief overview of the project and insights into certain design decisions.
本文将简要概述该项目,并深入了解某些设计决策。
Ethan-Arrowood/matterhornAn API boilerplate project built on Node.js and TypeScript ? - Ethan-Arrowood/matterhorngithub.com
Ethan-Arrowood / matterhorn 基于Node.js和TypeScript构建的API样板项目? -Ethan-Arrowood / matterhorng ithub.com
? Psst! This overview section is very similar to the project docs on GitHub
? s! 此概述部分与G itHub上的项目文档非常相似
Get started quickly by following these steps:
请按照以下步骤快速入门:
?♀️ Run npm run install && npm run dev
?️️运行n pm run install && npm run dev
? Edit any of the files in src/
? 编辑s rc/
中的任何文件
✨ That’s it for the basic user guide. Now let’s dive into some of the commands available to you by default. All of the commands below can be run with npm run <scri
pt> . This project makes use of npm modul
es opn and
rimraf to enable platform agnostic npm scripts.
for这就是基本的用户指南。 现在,让我们深入了解默认情况下可用的一些命令。 下面的所有命令都可以使用npm run <scri
pt>来运行。 该项目利用NPM莫dul
ES运算n and
rimraf使平台无关NPM脚本。
build
— build the TypeScript files and output to lib/
build
—构建TypeScript文件并输出到lib/
build:watch
— automatically rebuild files if changes are detected in src/
build:watch
—如果在src/
中检测到更改,则自动重建文件
clean
— recursively delete the lib/
and coverage/
directories
clean
—递归删除lib/
和coverage/
目录
clean:build
— recursively delete the lib/
directory
clean:build
—递归删除lib/
目录
clean:coverage
— recursively delete the coverage/
directory
clean:coverage
—递归删除coverage/
目录
coverage
— run the test suite and generate code coverage reports
coverage
—运行测试套件并生成代码覆盖率报告
coverage:open
— run npm run coverage
then open the results in a browser
coverage:open
—运行npm run coverage
然后在浏览器中打开结果
dev
— concurrently run build:watch
and start:watch
dev
—同时运行build:watch
和start:watch
lint
— run the linter configured by TSLint on the src/
directory
lint
—在src/
目录上运行TSLint配置的linter
start
— run the app from lib/
. Make sure to use npm run build
first!
start
-运行从应用程序lib/
。 确保首先使用npm run build
!
start:watch
— relaunch the server if new changes are detected in lib/
start:watch
—如果在lib/
中检测到新更改,则重新启动服务器
test
— run unit tests defined in the tests/
directory
test
—运行在tests/
目录中定义的单元测试
test:ci
— run unit tests and generate necessary files for CI integration
test:ci
—运行单元测试并生成用于CI集成的必要文件
Matterhorn implements example usage of both command line arguments and environment variables. It uses yargs-parser
to manage command line arguments. Command line arguments are passed in through the start command: node lib/index.js <command line argumen
ts>.
马特洪峰实现了命令行参数和环境变量的示例用法。 它使用yargs-parser
来管理命令行参数。 命令行参数通过以下启动命令传递: node lib/index.js <command line argumen
ts>。
The--log
argument has been enabled as an example. Running npm run start
starts up the project without any command line arguments. This command is intended to be used in production, so logging is disabled by default (i.e. we don’t pass the —-log
argument).
以--log
参数为例。 运行npm run start
启动没有任何命令行参数的项目。 该命令旨在用于生产环境,因此默认情况下禁用日志记录(即,我们不传递—-log
参数)。
If you are using this command to test your code locally and want to see the logging output, then run npm run start —- -—log
. This passes the command line argument through npm and into the aliased command.
如果要使用此命令在本地测试代码并希望查看日志记录输出,请运行npm run start —- -—log
。 这会将命令行参数通过npm传递到别名命令中。
Environment variables work in a similar way to command line arguments. They can be set in multiple ways depending on the terminal and operating system you are using. In a bash terminal you can specify environment variables as you use any of the above mentioned scripts by prepending the assignment to the command.
环境变量的工作方式与命令行参数类似。 可以根据所使用的终端和操作系统以多种方式设置它们。 在bash终端中,可以通过在命令之前添加分配来指定环境变量,以使用上述任何脚本。
For example, this project has the PORT
environment variable enabled. In a bash terminal run PORT=8080 npm run start
to run the API on port 8080.
例如,此项目已启用PORT
环境变量。 在bash终端中,运行PORT=8080 npm run start
以在端口8080上运行API。
I built this project because I found myself constantly copying and pasting config files for new Node.js projects. I love what the create-react-app
team has accomplished and envision Matterhorn developing into a similar kind of tool. Down the road, I look forward to developing a complete CLI to help developers get up and running with Node.js and TypeScript even quicker.
我之所以建立这个项目是因为我发现自己不断为新的Node.js项目复制和粘贴配置文件。 我喜欢create-react-app
团队所取得的成就,并希望Matterhorn能够发展成为一种类似的工具。 在此过程中,我期待开发一个完整的CLI,以帮助开发人员更快地使用Node.js和TypeScript并开始运行。
Matterhorn is an opinionated project. The build and linting systems are configured to my preferences, but are very easy to change. For example, in tslint.json
I defined the "semicolon"
rule as false
— to enforce semicolon usage throughout the app, change this to true
.
马特宏峰是一个自以为是的项目。 构建和棉绒系统可根据我的喜好进行配置,但很容易更改。 例如,在tslint.json
我将"semicolon"
tslint.json
"semicolon"
规则定义为false
-为了在整个应用程序中强制使用分号,请将其更改为true
。
Additionally, this project contains an azure-pipelines.yml
file. This defines the CI (continuous integration) pipeline on Azure DevOps, a robust tool offered by Microsoft to enable teams to plan smarter, collaborate better, and ship faster with a set of modern dev services. This was another opinionated decision due to my experience with the tool. There are many other great CI options such as Travis CI or Circle CI that I hope to support in the future.
此外,该项目还包含azure-pipelines.yml
文件。 这定义了Azure DevOps上的CI(连续集成)管道,Azure DevOps是Microsoft提供的一种强大工具,使团队可以使用一组现代开发服务来更明智地计划,更好地协作和更快地发布。 由于我使用该工具的经验,这是另一个自以为是的决定。 我希望将来能支持其他许多出色的CI选项,例如Travis CI或Circle CI。
Thank you for taking the time to read this article and checking out Matterhorn ?. The project is open sourced, and I encourage developers of any skill level to come contribute. Check it out on GitHub and if you want to hear about future updates as well as other things I develop follow me on Twitter.
感谢您抽出宝贵的时间阅读本文并查看Matterhorn?。 该项目是开源的,我鼓励任何技能水平的开发人员都可以贡献力量。 在G itHub上查看它,如果您想了解将来的更新以及我开发的其他内容,请在T witter上关注我。
Best wishes ? ~ Ethan Arrowood
最好的祝愿 ? 〜伊桑·阿罗德
约翰卡马特