@alexmc2/create-express-api-starter 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -83
- package/package.json +1 -1
- package/templates/ts/mvc/README.md.ejs +1 -0
- package/templates/ts/simple/README.md.ejs +1 -0
package/README.md
CHANGED
|
@@ -4,6 +4,12 @@
|
|
|
4
4
|
|
|
5
5
|
A beginner-friendly npm CLI that scaffolds Express APIs with best-practice structure and optional educational comments.
|
|
6
6
|
|
|
7
|
+
## About
|
|
8
|
+
|
|
9
|
+
Most Express projects require some initial setup, including folders, middleware, error handling, and testing. This CLI tool automates this process so you can start writing routes immediately. It includes a standard, readable codebase with a pre-configured Jest and Supertest suite that passes right out of the box.
|
|
10
|
+
|
|
11
|
+
The CLI offers multiple configuration options to suit different preferences and use cases, including:
|
|
12
|
+
|
|
7
13
|
- JavaScript or TypeScript
|
|
8
14
|
- Simple or MVC architecture
|
|
9
15
|
- In-memory or PostgreSQL (local `psql` or Docker)
|
|
@@ -15,35 +21,18 @@ A beginner-friendly npm CLI that scaffolds Express APIs with best-practice struc
|
|
|
15
21
|
|
|
16
22
|
<br />
|
|
17
23
|
|
|
18
|
-
>
|
|
24
|
+
> The CLI works on Linux, macOS, and Windows.
|
|
19
25
|
|
|
20
26
|
## Table of contents
|
|
21
27
|
|
|
22
|
-
- [
|
|
28
|
+
- [About](#about)
|
|
23
29
|
- [Table of contents](#table-of-contents)
|
|
24
30
|
- [Installation](#installation)
|
|
25
|
-
- [npm create (recommended)](#npm-create-recommended)
|
|
26
|
-
- [npm init](#npm-init)
|
|
27
|
-
- [npx (equivalent)](#npx-equivalent)
|
|
28
|
-
- [Global install (optional)](#global-install-optional)
|
|
29
31
|
- [Usage examples](#usage-examples)
|
|
30
|
-
- [Interactive flow](#interactive-flow)
|
|
31
|
-
- [Accept defaults (non-interactive)](#accept-defaults-non-interactive)
|
|
32
|
-
- [Dry run](#dry-run)
|
|
33
|
-
- [Why this exists](#why-this-exists)
|
|
34
32
|
- [How it works](#how-it-works)
|
|
35
33
|
- [Options](#options)
|
|
36
|
-
- [CLI flags](#cli-flags)
|
|
37
34
|
- [What gets generated](#what-gets-generated)
|
|
38
|
-
- [Project structure (Simple architecture, JavaScript)](#project-structure-simple-architecture-javascript)
|
|
39
|
-
- [Middleware](#middleware)
|
|
40
|
-
- [Error handling](#error-handling)
|
|
41
|
-
- [Scripts](#scripts)
|
|
42
|
-
- [Tests](#tests)
|
|
43
35
|
- [Database modes](#database-modes)
|
|
44
|
-
- [In-memory (default)](#in-memory-default)
|
|
45
|
-
- [Postgres (psql)](#postgres-psql)
|
|
46
|
-
- [Postgres (Docker)](#postgres-docker)
|
|
47
36
|
- [Educational comments](#educational-comments)
|
|
48
37
|
- [Design decisions](#design-decisions)
|
|
49
38
|
- [Requirements](#requirements)
|
|
@@ -77,14 +66,12 @@ npx @alexmc2/create-express-api-starter my-api
|
|
|
77
66
|
npm install -g @alexmc2/create-express-api-starter
|
|
78
67
|
```
|
|
79
68
|
|
|
80
|
-
> The npm package page may show `npm i @alexmc2/create-express-api-starter`. That command installs the package into your current project. For one-off scaffolding, use `npm create`, `npm init`, or `npx`.
|
|
81
|
-
|
|
82
69
|
## Usage examples
|
|
83
70
|
|
|
84
71
|
### Interactive flow
|
|
85
72
|
|
|
86
73
|
```bash
|
|
87
|
-
npm create @alexmc2/express-api-starter@latest
|
|
74
|
+
npm create @alexmc2/express-api-starter@latest
|
|
88
75
|
```
|
|
89
76
|
|
|
90
77
|
### Accept defaults (non-interactive)
|
|
@@ -109,28 +96,19 @@ npm test # Run the test suite
|
|
|
109
96
|
npm run lint # Run ESLint
|
|
110
97
|
```
|
|
111
98
|
|
|
112
|
-
Your API is live at `http://localhost:3000`.
|
|
113
|
-
|
|
114
|
-
## Why this exists
|
|
115
|
-
|
|
116
|
-
Express is unopinionated by design. That's great, but it means every new project starts with the same setup: folder structure, middleware wiring, error handlers, test runner, linting. This tool does all of that for you so you can skip straight to writing routes.
|
|
117
|
-
|
|
118
|
-
The generated project is simple enough to understand and modify. It's not a framework - it's a starting point that gets out of your way.
|
|
99
|
+
Your API is live at `http://localhost:3000`. Go to `http://localhost:3000/health` to confirm.
|
|
119
100
|
|
|
120
101
|
## How it works
|
|
121
102
|
|
|
122
|
-
|
|
103
|
+
The interactive CLI prompts you for configuration preferences and generates your project files from templates.
|
|
123
104
|
|
|
124
|
-
|
|
125
|
-
2. **The generated project** - a standalone Express API in its own folder with its own dependencies, scripts, and tests. The CLI exits after creating it.
|
|
126
|
-
|
|
127
|
-
When you run `npm create @alexmc2/express-api-starter@latest my-api` (or the equivalent `npm init`/`npx` command), npm downloads the CLI, generates the project into `my-api/`, optionally runs `npm install`, and exits. The generated project has no dependency on this tool.
|
|
105
|
+
When you run `npm create @alexmc2/express-api-starter@latest my-api`, npm downloads the CLI to build the project directly in your folder. You are left with a standalone Express API with its own scripts and tests, and the generated project has no ongoing dependency on this tool. You can safely uninstall the CLI globally if you used that installation method.
|
|
128
106
|
|
|
129
107
|
## Options
|
|
130
108
|
|
|
131
109
|
The CLI walks you through these choices interactively. Use `--yes` to skip prompts and accept all defaults.
|
|
132
110
|
|
|
133
|
-
| Option
|
|
111
|
+
| **Option** | **Choices** | **Default** |
|
|
134
112
|
| --------------------------- | --------------------------------------------- | -------------- |
|
|
135
113
|
| **Language** | JavaScript, TypeScript | JavaScript |
|
|
136
114
|
| **Module system** (JS only) | CommonJS, ES Modules | CommonJS |
|
|
@@ -143,7 +121,7 @@ The CLI walks you through these choices interactively. Use `--yes` to skip promp
|
|
|
143
121
|
|
|
144
122
|
### CLI flags
|
|
145
123
|
|
|
146
|
-
| Flag
|
|
124
|
+
| **Flag** | **Effect** |
|
|
147
125
|
| -------------- | ----------------------------------------------------------- |
|
|
148
126
|
| `--yes` | Accept all defaults, skip prompts |
|
|
149
127
|
| `--dry-run` | Show the generation plan without writing files |
|
|
@@ -155,7 +133,7 @@ The CLI walks you through these choices interactively. Use `--yes` to skip promp
|
|
|
155
133
|
|
|
156
134
|
### Project structure (Simple architecture, JavaScript)
|
|
157
135
|
|
|
158
|
-
```
|
|
136
|
+
```text
|
|
159
137
|
my-api/
|
|
160
138
|
├── src/
|
|
161
139
|
│ ├── app.js # Express app: middleware, routes, error handling
|
|
@@ -180,13 +158,13 @@ my-api/
|
|
|
180
158
|
└── jest.config.js
|
|
181
159
|
```
|
|
182
160
|
|
|
183
|
-
MVC
|
|
161
|
+
The MVC option organises the code into `controllers/`, `services/`, and `repositories/` to keep the business logic and data access separate.
|
|
184
162
|
|
|
185
163
|
Postgres modes additionally include `db/schema.sql`, `db/seed.sql`, `src/db/` (connection pool), and setup scripts in `scripts/`. Docker mode adds a `compose.yaml`.
|
|
186
164
|
|
|
187
165
|
### Middleware
|
|
188
166
|
|
|
189
|
-
|
|
167
|
+
The following middleware is pre-configured in every project:
|
|
190
168
|
|
|
191
169
|
- **express.json()** - parses JSON request bodies
|
|
192
170
|
- **cors** - enables cross-origin requests
|
|
@@ -205,7 +183,7 @@ Errors return a consistent JSON shape:
|
|
|
205
183
|
}
|
|
206
184
|
```
|
|
207
185
|
|
|
208
|
-
In development, a `stack` trace is included for debugging.
|
|
186
|
+
In development, a `stack` trace is included for debugging. This is omitted in production.
|
|
209
187
|
|
|
210
188
|
In PostgreSQL mode, duplicate values for unique fields (like `email`) return `409 Conflict` with a clear message instead of a generic `500`.
|
|
211
189
|
|
|
@@ -213,7 +191,7 @@ In PostgreSQL mode, duplicate values for unique fields (like `email`) return `40
|
|
|
213
191
|
|
|
214
192
|
**JavaScript projects:**
|
|
215
193
|
|
|
216
|
-
| Script
|
|
194
|
+
| **Script** | **Command** | **Purpose** |
|
|
217
195
|
| -------------- | --------------------------------------------------------------- | ---------------------------- |
|
|
218
196
|
| `npm run dev` | `node --watch src/server.js` or `nodemon src/server.js` | Dev server with auto-restart |
|
|
219
197
|
| `npm start` | `node src/server.js` | Production start |
|
|
@@ -224,7 +202,7 @@ Choosing `nodemon` adds it to `devDependencies` automatically.
|
|
|
224
202
|
|
|
225
203
|
**TypeScript projects:**
|
|
226
204
|
|
|
227
|
-
| Script
|
|
205
|
+
| **Script** | **Command** | **Purpose** |
|
|
228
206
|
| --------------- | ------------------------- | ---------------------------- |
|
|
229
207
|
| `npm run dev` | `tsx watch src/server.ts` | Dev server with auto-restart |
|
|
230
208
|
| `npm run build` | `tsc` | Compile to JavaScript |
|
|
@@ -234,7 +212,7 @@ Choosing `nodemon` adds it to `devDependencies` automatically.
|
|
|
234
212
|
|
|
235
213
|
**Postgres modes also include:**
|
|
236
214
|
|
|
237
|
-
| Script
|
|
215
|
+
| **Script** | **Purpose** |
|
|
238
216
|
| ------------------- | ------------------------------------------------- |
|
|
239
217
|
| `npm run db:create` | Create the database (psql mode only) |
|
|
240
218
|
| `npm run db:setup` | Apply `db/schema.sql` |
|
|
@@ -245,29 +223,24 @@ Choosing `nodemon` adds it to `devDependencies` automatically.
|
|
|
245
223
|
|
|
246
224
|
### Tests
|
|
247
225
|
|
|
248
|
-
|
|
226
|
+
The project includes a functional test suite using Jest and Supertest. In-memory mode works out of the box - just run `npm test` immediately after generation.
|
|
249
227
|
|
|
250
228
|
## Database modes
|
|
251
229
|
|
|
252
230
|
### In-memory (default)
|
|
253
231
|
|
|
254
|
-
Data
|
|
255
|
-
|
|
256
|
-
Good for learning the project structure and getting something running fast. The code uses a repository pattern, so switching to a real database later is straightforward.
|
|
232
|
+
Data is stored in a simple JavaScript array, so you don't need to install or configure anything. This is useful for prototyping, though data will reset every time the server restarts. Because the project uses a repository pattern, you can swap this for a real database later without having to refactor your routes.
|
|
257
233
|
|
|
258
234
|
### Postgres (psql)
|
|
259
235
|
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
- `pg` as a dependency with a connection pool
|
|
263
|
-
- `db/schema.sql` and `db/seed.sql` for table creation and sample data
|
|
264
|
-
- Node scripts for database management (`db:create`, `db:setup`, `db:seed`, `db:reset`) using the `pg` library
|
|
265
|
-
- A project-specific database name derived from your project name (e.g. `my-api` → `my_api_dev`)
|
|
266
|
-
- `DATABASE_URL` pre-configured with your OS username
|
|
236
|
+
If you have PostgreSQL running locally, this mode sets up everything you need to connect your app to a real database. The generated project includes:
|
|
267
237
|
|
|
268
|
-
|
|
238
|
+
- `pg` (node-postgres) with a pre-configured connection pool.
|
|
239
|
+
- `db/schema.sql` and `db/seed.sql` for your initial tables and test data.
|
|
240
|
+
- Management scripts (`db:setup`, `db:reset`, etc.) so you don't have to jump into the terminal to run SQL manually.
|
|
241
|
+
- A `DATABASE_URL` in your `.env` pre-filled with your OS username and a database name derived from your project.
|
|
269
242
|
|
|
270
|
-
**
|
|
243
|
+
**To get started:**
|
|
271
244
|
|
|
272
245
|
```bash
|
|
273
246
|
# After generation
|
|
@@ -281,11 +254,9 @@ The generated README includes OS-specific PostgreSQL installation and role setup
|
|
|
281
254
|
|
|
282
255
|
### Postgres (Docker)
|
|
283
256
|
|
|
284
|
-
|
|
257
|
+
If you prefer Docker to avoid a local installation, this mode provides a `compose.yaml` that runs PostgreSQL on port 5433 (avoiding conflicts with any local Postgres you might have).
|
|
285
258
|
|
|
286
|
-
|
|
287
|
-
- Node-based setup scripts using the `pg` library - no `psql` CLI needed on your machine
|
|
288
|
-
- A retry helper that waits for the database container to be ready before running setup
|
|
259
|
+
It includes a **retry helper** in the setup scripts. This solves the common issue where scripts fail because the database container isn't fully "ready" to accept connections yet.
|
|
289
260
|
|
|
290
261
|
```bash
|
|
291
262
|
# After generation
|
|
@@ -300,36 +271,26 @@ npm run db:down # Stop and remove container + data
|
|
|
300
271
|
|
|
301
272
|
## Educational comments
|
|
302
273
|
|
|
303
|
-
|
|
274
|
+
You can enable inline comments that explain the 'why' behind the code. These are designed to be short and useful, covering things like:
|
|
304
275
|
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
app.use(express.json());
|
|
309
|
-
|
|
310
|
-
// Set common HTTP security headers.
|
|
311
|
-
// Helmet applies safe defaults that reduce exposure to common web attacks.
|
|
312
|
-
app.use(helmet());
|
|
313
|
-
```
|
|
314
|
-
|
|
315
|
-
These comments cover middleware, routing, error handling, database scripts, and architecture patterns. They teach without getting in the way. Turn them off during generation if you prefer clean code.
|
|
276
|
+
- Why we use `express.json()` and what happens if you forget it.
|
|
277
|
+
- Why `helmet` is included for security.
|
|
278
|
+
- How the repository pattern simplifies your data logic.
|
|
316
279
|
|
|
317
280
|
## Design decisions
|
|
318
281
|
|
|
319
|
-
**Opinionated
|
|
320
|
-
|
|
321
|
-
**Module system is a choice (JS only).** CommonJS is the default because most Express tutorials use it. ES Modules are available if you prefer modern `import`/`export` syntax. TypeScript projects always use ESM-style imports that compile to CommonJS.
|
|
282
|
+
**Opinionated but swappable.** The tool uses a standard stack (Jest, Morgan, Helmet, and ESLint) so projects are functional immediately. There is no complex logic connecting these tools, so they are easy to swap out if a different library is preferred.
|
|
322
283
|
|
|
323
|
-
**
|
|
284
|
+
**Module system choice.** JavaScript projects allow choosing between CommonJS and ES Modules. TypeScript projects use ESM-style imports that compile to clean CommonJS.
|
|
324
285
|
|
|
325
|
-
**
|
|
286
|
+
**Auth, ORMs, and OpenAPI.** These are not included in the current templates. Since these are major architectural decisions, they are best left to the developer to decide based on specific project needs. These may be added as configurable options in later versions.
|
|
326
287
|
|
|
327
288
|
## Requirements
|
|
328
289
|
|
|
329
|
-
- **Node.js >= 20**
|
|
330
|
-
- **npm** (
|
|
331
|
-
- **Docker** (only
|
|
332
|
-
- **PostgreSQL** (only
|
|
290
|
+
- **Node.js >= 20**
|
|
291
|
+
- **npm** (comes with Node)
|
|
292
|
+
- **Docker** (only if using Docker DB mode)
|
|
293
|
+
- **PostgreSQL** (only if using local psql mode)
|
|
333
294
|
|
|
334
295
|
## Built with
|
|
335
296
|
|
|
@@ -342,7 +303,7 @@ These comments cover middleware, routing, error handling, database scripts, and
|
|
|
342
303
|
|
|
343
304
|
**Generated projects use:**
|
|
344
305
|
|
|
345
|
-
- Express
|
|
306
|
+
- Express 5
|
|
346
307
|
- Jest + Supertest for testing
|
|
347
308
|
- ESLint for linting
|
|
348
309
|
- `@swc/jest` for TypeScript test transforms
|
|
@@ -350,7 +311,7 @@ These comments cover middleware, routing, error handling, database scripts, and
|
|
|
350
311
|
|
|
351
312
|
## Contributing
|
|
352
313
|
|
|
353
|
-
|
|
314
|
+
If you find a bug or have an idea for a template, feel free to open an issue or a PR. See [CONTRIBUTING.md](CONTRIBUTING.md) for details.
|
|
354
315
|
|
|
355
316
|
```bash
|
|
356
317
|
git clone https://github.com/alexmc2/create-express-api-starter.git
|
package/package.json
CHANGED