create-uk-node-server 1.0.10 → 1.1.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/README.md CHANGED
@@ -57,7 +57,7 @@ your-project/
57
57
 
58
58
  ```
59
59
  APP_ENV=development
60
- APP_PORT=3000
60
+ PORT=3000
61
61
  APP_URL=http://localhost:3000
62
62
  DB_URL=mongodb://localhost:27017/your-database
63
63
  ```
package/bin/cli.js CHANGED
@@ -1,4 +1,4 @@
1
- #!/usr/bin/env node
1
+ #!/usr/bin/env node
2
2
 
3
3
  import main from "../index.js";
4
4
  main();
package/index.js CHANGED
@@ -31,9 +31,10 @@ async function main() {
31
31
  const pkg = fs.readJsonSync(pkgPath);
32
32
  pkg.name = answers.projectName;
33
33
  fs.writeJsonSync(pkgPath, pkg, { spaces: 2 });
34
+ console.log("Installing dependencies...")
34
35
  execSync(`cd ${projectPath} && npm install`);
35
36
  console.log('success!')
36
- console.log(`cd ${answers.projectName} and change DB connection in .env file and run npm start`)
37
+ console.log(`create .env file and add DB_URL="your-db-connection-string" && run npm start`)
37
38
  })
38
39
  }
39
40
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-uk-node-server",
3
- "version": "1.0.10",
3
+ "version": "1.1.0",
4
4
  "description": "A CLI tool to quickly scaffold server projects with JavaScript or TypeScript templates",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,18 +1,20 @@
1
1
  import express from "express"
2
- import { connectDB } from "./src/config/db.config.js"
2
+ import dotenv from 'dotenv';
3
+ dotenv.config();
4
+ import indexConfig from "./src/config/index.config.js"
3
5
  import indexRoutes from "./src/routes/index.routes.js"
4
- import { errorHandler } from "./src/middlewares/errorHandler.js"
6
+ import indexMiddleware from "./src/middlewares/index.middleware.js"
5
7
 
6
8
  const app = express()
7
- const PORT = 3000
9
+ const PORT = process.env.PORT || 3000;
8
10
 
9
11
  app.use(express.json());
10
12
  app.use(express.urlencoded({ extended: true }));
11
- app.use(errorHandler);
13
+ app.use(indexMiddleware.errorHandler);
12
14
 
13
15
  app.use("/api", indexRoutes)
14
16
 
15
- connectDB()
17
+ indexConfig.connectDB()
16
18
  .then(() => {
17
19
  app.listen(PORT, () => {
18
20
  console.log(`Server is running on port ${PORT}`)
@@ -0,0 +1,5 @@
1
+ import { connectDB } from "./db.config.js"
2
+
3
+ export default {
4
+ connectDB
5
+ }
@@ -0,0 +1,5 @@
1
+ import { errorHandler } from "./errorHandler.js"
2
+
3
+ export default {
4
+ errorHandler
5
+ }
@@ -0,0 +1,5 @@
1
+ import User from "./user.model.js"
2
+
3
+ export default {
4
+ User
5
+ }
@@ -1,6 +1,6 @@
1
1
  import express from "express";
2
2
  import { test } from "../controllers/index.controller.js"
3
- import userRouter from "./uesr.routes.js"
3
+ import userRouter from "./user.routes.js"
4
4
  const router = express.Router();
5
5
 
6
6
  router.get("/test", test);
@@ -24,5 +24,6 @@
24
24
  },
25
25
  "keywords": [],
26
26
  "author": "",
27
- "license": "ISC"
27
+ "license": "ISC",
28
+ "type": "module"
28
29
  }
@@ -0,0 +1,5 @@
1
+ import { connectDB } from "./db.config.js";
2
+
3
+ export default {
4
+ connectDB
5
+ }
@@ -0,0 +1,11 @@
1
+ import { Request, Response } from "express";
2
+ import { errorResponse, successResponse } from "../utils/general.js";
3
+
4
+ export const test = async (req: Request, res: Response) => {
5
+ try {
6
+ return res.json(successResponse("Hello from test", {}))
7
+ } catch (error) {
8
+ console.log("Error in test--------->", error)
9
+ return res.json(errorResponse("Something went wrong", error))
10
+ }
11
+ }
@@ -1,34 +1,28 @@
1
1
  import express from 'express';
2
2
  import cors from 'cors';
3
3
  import dotenv from 'dotenv';
4
- import { connectDB } from './utils/db.config.js';
4
+ import indexConfig from './config/index.config.js';
5
5
  import indexRoutes from './routes/index.route.js';
6
+ import indexMiddleware from './middlewares/index.middleware.js';
6
7
 
7
8
  dotenv.config();
8
9
 
9
10
  const app = express();
10
- const PORT = process.env.APP_PORT || 3000;
11
+ const PORT = process.env.PORT || 3000;
11
12
 
12
13
  app.use(cors());
13
14
  app.use(express.json());
14
15
  app.use(express.urlencoded({ extended: true }));
15
-
16
- app.get('/', (req, res) => {
17
- res.json({ message: 'Welcome to TypeScript Server!' });
18
- });
16
+ app.use(indexMiddleware.errorHandler);
19
17
 
20
18
  app.use('/api', indexRoutes);
21
19
 
22
- const startServer = async () => {
23
- try {
24
- await connectDB();
25
- app.listen(PORT, () => {
26
- console.log(`Server is running on port ${PORT}`);
27
- });
28
- } catch (error) {
29
- console.error('Failed to start server:', error);
30
- process.exit(1);
31
- }
32
- };
33
-
34
- startServer();
20
+ indexConfig.connectDB()
21
+ .then(() => {
22
+ app.listen(PORT, () => {
23
+ console.log(`Server is running on port ${PORT}`)
24
+ })
25
+ }).catch((error) => {
26
+ console.error("Error connecting to MongoDB:", error)
27
+ process.exit(1)
28
+ })
@@ -0,0 +1,9 @@
1
+ import { Request, Response, NextFunction } from "express";
2
+
3
+ export function errorHandler(err: Error, req: Request, res: Response, next: NextFunction) {
4
+ res.status(500).json({
5
+ flag: 0,
6
+ msg: err.message,
7
+ data: null
8
+ })
9
+ }
@@ -0,0 +1,5 @@
1
+ import { errorHandler } from "./errorHandler.js";
2
+
3
+ export default {
4
+ errorHandler
5
+ }
@@ -0,0 +1,5 @@
1
+ import User from "./user.model.js"
2
+
3
+ export default {
4
+ User
5
+ }
@@ -1,9 +1,10 @@
1
1
  import { Router, Request, Response } from 'express';
2
+ import { test} from "../controllers/index.controller.js"
3
+ import userRouter from "./user.routes.js"
2
4
 
3
5
  const router = Router();
4
6
 
5
- router.get('/', (req: Request, res: Response) => {
6
- res.json({ message: 'API is working!' });
7
- });
7
+ router.get('/', test);
8
+ router.use("/users", userRouter)
8
9
 
9
10
  export default router;
@@ -0,0 +1,11 @@
1
+ import { Router, Request, Response } from "express"
2
+
3
+ const router = Router()
4
+
5
+ router.get("/list", (req: Request, res: Response) => {
6
+ return res.json({
7
+ message: "users Found Successfully..."
8
+ })
9
+ })
10
+
11
+ export default router;
@@ -1,60 +0,0 @@
1
- export default function createPackageJsonFile(projectPath, projectName, language) {
2
- let packageJson = `
3
- {
4
- "name": "${projectName}",
5
- "version": "1.0.0",
6
- "description": "",
7
- "main": "index.js",
8
- "scripts": {
9
- "start": "node index.js",
10
- "dev": "nodemon index.js",
11
- "test": "echo \"Error: no test specified\" && exit 1"
12
- },
13
- "keywords": [],
14
- "author": "",
15
- "license": "ISC",
16
- "type": "module",
17
- "dependencies": {
18
- "bcrypt": "^6.0.0",
19
- "dotenv": "^16.5.0",
20
- "express": "^5.1.0",
21
- "mongoose": "^8.15.0",
22
- "nodemon": "^3.1.10"
23
- }
24
- }
25
- `
26
- // if language is typescript
27
- if (language === "typescript") {
28
- packageJson = `
29
- {
30
- "name": "${projectName}",
31
- "version": "1.0.0",
32
- "description": "TypeScript server template",
33
- "main": "dist/index.js",
34
- "scripts": {
35
- "start": "node dist/index.js",
36
- "dev": "ts-node src/index.ts",
37
- "build": "tsc",
38
- "watch": "tsc --watch"
39
- },
40
- "dependencies": {
41
- "express": "^4.18.2",
42
- "dotenv": "^16.3.1",
43
- "cors": "^2.8.5",
44
- "mongoose": "^8.0.3"
45
- },
46
- "devDependencies": {
47
- "@types/express": "^4.17.21",
48
- "@types/node": "^20.10.0",
49
- "@types/cors": "^2.8.17",
50
- "typescript": "^5.3.2",
51
- "ts-node": "^10.9.1"
52
- },
53
- "keywords": [],
54
- "author": "",
55
- "license": "ISC"
56
- }
57
- `
58
- }
59
- fs.writeFileSync(path.join(projectPath, "package.json"), packageJson)
60
- }