express-scaffolding-typescript 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Qiuyi Hong
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
File without changes
package/bin/cli.js ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+
3
+ import "../lib/cli.js";
package/lib/cli.js ADDED
@@ -0,0 +1,35 @@
1
+ import { resolve } from "node:path";
2
+ import expressGenTs from "./express-scaffolding-typescript.js";
3
+
4
+ /******************************************************************************
5
+ Run
6
+ ******************************************************************************/
7
+
8
+ // Init
9
+ console.log("Setting up new Express/TypeScript project...");
10
+ const args = process.argv.slice(2);
11
+
12
+ // Setup use yarn
13
+ let useYarn = false;
14
+ const useYarnIdx = args.indexOf("--use-yarn");
15
+ if (useYarnIdx > -1) {
16
+ useYarn = true;
17
+ args.splice(useYarnIdx, 1);
18
+ }
19
+
20
+ // Setup destination
21
+ let destination = "express-scaffolding-ts";
22
+ if (args.length > 0) {
23
+ destination = args[0];
24
+ }
25
+ destination = resolve(process.cwd(), destination);
26
+
27
+ // Creating new project finished
28
+ expressGenTs(destination, useYarn)
29
+ .then(() => {
30
+ console.log("Project setup complete!");
31
+ })
32
+ .catch((err) => {
33
+ console.error(err);
34
+ process.exitCode = 1;
35
+ });
@@ -0,0 +1,125 @@
1
+ import { existsSync } from "node:fs";
2
+ import { rename } from "node:fs/promises";
3
+ import { execSync } from "node:child_process";
4
+ import { basename, dirname, join, relative } from "node:path";
5
+ import { fileURLToPath } from "node:url";
6
+ import editJsonFile from "edit-json-file";
7
+ import ncpPackage from "ncp";
8
+
9
+ /******************************************************************************
10
+ Constants
11
+ ******************************************************************************/
12
+
13
+ const MODULE_DIR = dirname(fileURLToPath(import.meta.url));
14
+ const PROJECT_FOLDER_PATH = join(MODULE_DIR, "projectFolder");
15
+ const { ncp } = ncpPackage;
16
+ const EXCLUDED_TEMPLATE_ENTRIES = new Set([
17
+ ".npmignore",
18
+ "node_modules",
19
+ "package-lock.json",
20
+ ]);
21
+
22
+ const DEPENDENCIES = ["body-parser", "dotenv", "express", "path", "uuid"];
23
+
24
+ const DEV_DEPENDENCIES = [
25
+ "@eslint/js",
26
+ "@types/express",
27
+ "@types/node",
28
+ "eslint",
29
+ "eslint-config-prettier",
30
+ "eslint-plugin-prettier",
31
+ "globals",
32
+ "jiti",
33
+ "prettier",
34
+ "tsx",
35
+ "typescript",
36
+ "typescript-eslint",
37
+ ];
38
+
39
+ // "ncp" options
40
+ const ncpOpts = {
41
+ filter: (fileName) => {
42
+ return !relative(PROJECT_FOLDER_PATH, fileName)
43
+ .split(/[\\/]/)
44
+ .some((entry) => EXCLUDED_TEMPLATE_ENTRIES.has(entry));
45
+ },
46
+ };
47
+
48
+ /******************************************************************************
49
+ Functions
50
+ ******************************************************************************/
51
+
52
+ /**
53
+ * Entry point
54
+ */
55
+ async function expressGenTs(destination, useYarn) {
56
+ await copyProjectFiles(destination);
57
+ updatePackageJson(destination);
58
+ await renameGitignoreFile(destination);
59
+ downloadNodeModules(destination, useYarn);
60
+ }
61
+
62
+ /**
63
+ * Copy project files
64
+ */
65
+ function copyProjectFiles(destination) {
66
+ const source = PROJECT_FOLDER_PATH;
67
+ return /** @type {Promise<void>} */ (
68
+ new Promise((res, rej) => {
69
+ return ncp(source, destination, ncpOpts, (err) => {
70
+ return err ? rej(err) : res();
71
+ });
72
+ })
73
+ );
74
+ }
75
+
76
+ /**
77
+ * Set update the package.json file.
78
+ */
79
+ function updatePackageJson(destination) {
80
+ let file = editJsonFile(destination + "/package.json", {
81
+ autosave: true,
82
+ });
83
+ file.set("name", basename(destination));
84
+ file.set("dependencies", {});
85
+ file.set("devDependencies", {});
86
+ }
87
+
88
+ /**
89
+ * Because npm does not allow .gitignore to be published.
90
+ */
91
+ async function renameGitignoreFile(destination) {
92
+ const source = join(destination, "gitignore");
93
+ if (!existsSync(source)) {
94
+ return;
95
+ }
96
+ await rename(source, join(destination, ".gitignore"));
97
+ }
98
+
99
+ /**
100
+ * Download the dependencies.
101
+ */
102
+ function downloadNodeModules(destination, useYarn) {
103
+ const options = { cwd: destination };
104
+ // Setup dependencies string
105
+ let depStr = DEPENDENCIES.join(" "),
106
+ devDepStr = DEV_DEPENDENCIES.join(" ");
107
+ // Setup download command
108
+ let downloadLibCmd, downloadDepCmd;
109
+ if (useYarn) {
110
+ downloadLibCmd = "yarn add " + depStr;
111
+ downloadDepCmd = "yarn add " + devDepStr + " -D";
112
+ } else {
113
+ downloadLibCmd = "npm i -s " + depStr;
114
+ downloadDepCmd = "npm i -D " + devDepStr;
115
+ }
116
+ // Execute command
117
+ execSync(downloadLibCmd, options);
118
+ execSync(downloadDepCmd, options);
119
+ }
120
+
121
+ /******************************************************************************
122
+ Export
123
+ ******************************************************************************/
124
+
125
+ export default expressGenTs;
@@ -0,0 +1,2 @@
1
+ PORT=3000
2
+ NODE_ENV=development
@@ -0,0 +1,24 @@
1
+ import js from "@eslint/js";
2
+ import globals from "globals";
3
+ import tseslint from "typescript-eslint";
4
+ import { defineConfig, globalIgnores } from "eslint/config";
5
+ import eslintPluginPrettierRecommended from "eslint-plugin-prettier/recommended";
6
+
7
+ export default defineConfig([
8
+ globalIgnores(["dist"]),
9
+ {
10
+ files: ["**/*.{js,mjs,cjs,ts,mts,cts}"],
11
+ plugins: { js },
12
+ extends: [
13
+ js.configs.recommended,
14
+ tseslint.configs.recommended,
15
+ eslintPluginPrettierRecommended,
16
+ ],
17
+ languageOptions: { globals: { ...globals.browser, ...globals.node } },
18
+ rules: {
19
+ "prettier/prettier": "warn",
20
+ "no-unused-vars": "warn",
21
+ "@typescript-eslint/no-unused-vars": "warn",
22
+ },
23
+ },
24
+ ]);
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "Express.js project",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "license": "ISC",
6
+ "author": "Qiuyi Hong",
7
+ "type": "module",
8
+ "main": "server.ts",
9
+ "scripts": {
10
+ "dev": "tsx --watch src/server.ts",
11
+ "start": "node dist/server.js",
12
+ "build": "tsc",
13
+ "type-check": "tsc --noEmit",
14
+ "lint": "eslint .",
15
+ "lint:fix": "eslint --fix .",
16
+ "format": "prettier --write .",
17
+ "format:check": "prettier --check .",
18
+ "test": "echo \"Error: no test specified\" && exit 1"
19
+ },
20
+ "devDependencies": {
21
+ "@eslint/js": "^10.0.1",
22
+ "@types/express": "^5.0.6",
23
+ "@types/node": "^26.0.1",
24
+ "eslint": "^10.6.0",
25
+ "eslint-config-prettier": "^10.1.8",
26
+ "eslint-plugin-prettier": "^5.5.6",
27
+ "globals": "^17.7.0",
28
+ "jiti": "^2.7.0",
29
+ "prettier": "3.9.4",
30
+ "tsx": "^4.22.4",
31
+ "typescript": "^6.0.3",
32
+ "typescript-eslint": "^8.62.1"
33
+ },
34
+ "dependencies": {
35
+ "body-parser": "^2.3.0",
36
+ "dotenv": "^17.4.2",
37
+ "express": "^5.2.1",
38
+ "path": "^0.12.7",
39
+ "uuid": "^14.0.1"
40
+ }
41
+ }
@@ -0,0 +1,30 @@
1
+ import express from "express";
2
+ import itemRoutes from "./routes/itemRoutes.ts";
3
+ import { errorHandler } from "./middlewares/errorHandler.ts";
4
+ import bodyParser from "body-parser";
5
+
6
+ const app = express();
7
+
8
+ app.use(bodyParser.json());
9
+ app.use(express.static("public"));
10
+
11
+ app.use((req, res, next) => {
12
+ res.setHeader("Access-Control-Allow-Origin", "*");
13
+ res.setHeader(
14
+ "Access-Control-Allow-Methods",
15
+ "GET, POST, PUT, DELETE, OPTIONS",
16
+ );
17
+ res.setHeader(
18
+ "Access-Control-Allow-Headers",
19
+ "X-Requested-With,content-type",
20
+ );
21
+ next();
22
+ });
23
+
24
+ // Routes
25
+ app.use("/items", itemRoutes);
26
+
27
+ // Global error handler (should be after routes)
28
+ app.use(errorHandler);
29
+
30
+ export default app;
@@ -0,0 +1,15 @@
1
+ import dotenv from "dotenv";
2
+
3
+ dotenv.config();
4
+
5
+ interface Config {
6
+ port: number;
7
+ nodeEnv: string;
8
+ }
9
+
10
+ const config: Config = {
11
+ port: Number(process.env.PORT) || 3000,
12
+ nodeEnv: process.env.NODE_ENV || "development",
13
+ };
14
+
15
+ export default config;
@@ -0,0 +1,78 @@
1
+ import type { Request, Response, NextFunction } from "express";
2
+ import { items, type Item } from "../models/item.ts";
3
+ import { v4 as uuidv4 } from "uuid";
4
+
5
+ // Create an item
6
+ export const createItem = (req: Request, res: Response, next: NextFunction) => {
7
+ try {
8
+ const { name } = req.body;
9
+ const newItem: Item = { id: uuidv4(), name };
10
+ items.push(newItem);
11
+ res.status(201).json(newItem);
12
+ } catch (error) {
13
+ next(error);
14
+ }
15
+ };
16
+
17
+ // Read all items
18
+ export const getItems = (req: Request, res: Response, next: NextFunction) => {
19
+ try {
20
+ res.json(items);
21
+ } catch (error) {
22
+ next(error);
23
+ }
24
+ };
25
+
26
+ // Read single item
27
+ export const getItemById = (
28
+ req: Request,
29
+ res: Response,
30
+ next: NextFunction,
31
+ ) => {
32
+ try {
33
+ const id = req.params.id as string;
34
+ const item = items.find((i) => i.id === id);
35
+ if (!item) {
36
+ res.status(404).json({ message: "Item not found" });
37
+ return;
38
+ }
39
+ res.json(item);
40
+ } catch (error) {
41
+ next(error);
42
+ }
43
+ };
44
+
45
+ // Update an item
46
+ export const updateItem = (req: Request, res: Response, next: NextFunction) => {
47
+ try {
48
+ const id = req.params.id as string;
49
+ const { name } = req.body;
50
+ const itemIndex = items.findIndex((i) => i.id === id);
51
+ if (itemIndex === -1) {
52
+ res.status(404).json({ message: "Item not found" });
53
+ return;
54
+ }
55
+ if (name !== undefined) {
56
+ items[itemIndex]!.name = name;
57
+ }
58
+ res.json(items[itemIndex]);
59
+ } catch (error) {
60
+ next(error);
61
+ }
62
+ };
63
+
64
+ // Delete an item
65
+ export const deleteItem = (req: Request, res: Response, next: NextFunction) => {
66
+ try {
67
+ const id = req.params.id as string;
68
+ const itemIndex = items.findIndex((i) => i.id === id);
69
+ if (itemIndex === -1) {
70
+ res.status(404).json({ message: "Item not found" });
71
+ return;
72
+ }
73
+ const deletedItem = items.splice(itemIndex, 1)[0];
74
+ res.json(deletedItem);
75
+ } catch (error) {
76
+ next(error);
77
+ }
78
+ };
@@ -0,0 +1,18 @@
1
+ import type { Request, Response, NextFunction } from "express";
2
+
3
+ export interface AppError extends Error {
4
+ status?: number;
5
+ }
6
+
7
+ export const errorHandler = (
8
+ err: AppError,
9
+ req: Request,
10
+ res: Response,
11
+ next: NextFunction,
12
+ ) => {
13
+ void next;
14
+ console.error(err);
15
+ res.status(err.status || 500).json({
16
+ message: err.message || "Internal Server Error",
17
+ });
18
+ };
@@ -0,0 +1,6 @@
1
+ export interface Item {
2
+ id: string;
3
+ name: string;
4
+ }
5
+
6
+ export const items: Item[] = [];
@@ -0,0 +1,18 @@
1
+ import { Router } from "express";
2
+ import {
3
+ createItem,
4
+ getItems,
5
+ getItemById,
6
+ updateItem,
7
+ deleteItem,
8
+ } from "../controllers/itemController.ts";
9
+
10
+ const router = Router();
11
+
12
+ router.get("/", getItems);
13
+ router.get("/:id", getItemById);
14
+ router.post("/", createItem);
15
+ router.put("/:id", updateItem);
16
+ router.delete("/:id", deleteItem);
17
+
18
+ export default router;
@@ -0,0 +1,6 @@
1
+ import app from "./app.ts";
2
+ import config from "./config/config.ts";
3
+
4
+ app.listen(config.port, () => {
5
+ console.log(`Server running on port ${config.port}`);
6
+ });
@@ -0,0 +1,51 @@
1
+ {
2
+ // Visit https://aka.ms/tsconfig to read more about this file
3
+ "compilerOptions": {
4
+ // File Layout
5
+ "rootDir": "./src",
6
+ "outDir": "./dist",
7
+
8
+ // Environment Settings
9
+ // See also https://aka.ms/tsconfig/module
10
+ "module": "nodenext",
11
+ "target": "esnext",
12
+ // "types": [],
13
+ // For nodejs:
14
+ "lib": ["esnext"],
15
+ "types": ["node"],
16
+ // and npm install -D @types/node
17
+
18
+ // Other Outputs
19
+ "sourceMap": true,
20
+ "declaration": true,
21
+ "declarationMap": true,
22
+
23
+ // Stricter Typechecking Options
24
+ "noUncheckedIndexedAccess": true,
25
+ "exactOptionalPropertyTypes": true,
26
+
27
+ // Style Options
28
+ // "noImplicitReturns": true,
29
+ // "noImplicitOverride": true,
30
+ // "noUnusedLocals": true,
31
+ // "noUnusedParameters": true,
32
+ // "noFallthroughCasesInSwitch": true,
33
+ // "noPropertyAccessFromIndexSignature": true,
34
+
35
+ // Recommended Options
36
+ "strict": true,
37
+ "jsx": "react-jsx",
38
+ "verbatimModuleSyntax": true,
39
+ "isolatedModules": true,
40
+ "noUncheckedSideEffectImports": true,
41
+ "moduleDetection": "force",
42
+ "skipLibCheck": true,
43
+
44
+ // Additional Options
45
+ "esModuleInterop": true,
46
+ "allowImportingTsExtensions": true,
47
+ "noEmit": true
48
+ }
49
+ // "include": ["src"],
50
+ // "exclude": ["dist"]
51
+ }
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "express-scaffolding-typescript",
3
+ "version": "1.0.0",
4
+ "description": "Scaffold new Express.js application with TypeScript",
5
+ "keywords": [
6
+ "express"
7
+ ],
8
+ "license": "MIT",
9
+ "author": "Qiuyi Hong",
10
+ "type": "module",
11
+ "main": "lib/express-scaffolding-typescript.js",
12
+ "bin": {
13
+ "express-scaffolding-typescript": "bin/cli.js"
14
+ },
15
+ "files": [
16
+ "bin",
17
+ "lib"
18
+ ],
19
+ "directories": {
20
+ "lib": "lib"
21
+ },
22
+ "scripts": {
23
+ "start": "node bin/cli.js",
24
+ "lint": "eslint bin lib/*.js test eslint.config.mjs",
25
+ "test": "node --test"
26
+ },
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "https://github.com/Qiuyi-Hong/express-scaffolding-typescript.git"
30
+ },
31
+ "homepage": "https://github.com/Qiuyi-Hong/express-scaffolding-typescript#readme",
32
+
33
+ "devDependencies": {
34
+ "@eslint/js": "^10.0.1",
35
+ "eslint": "^10.6.0",
36
+ "eslint-config-prettier": "^10.1.8",
37
+ "eslint-plugin-prettier": "^5.5.6",
38
+ "globals": "^17.7.0",
39
+ "prettier": "3.9.4",
40
+ "typescript": "^6.0.3",
41
+ "typescript-eslint": "^8.62.1"
42
+ },
43
+ "dependencies": {
44
+ "edit-json-file": "^1.8.1",
45
+ "ncp": "^2.0.0"
46
+ }
47
+ }