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 +1 -1
- package/bin/cli.js +1 -1
- package/index.js +2 -1
- package/package.json +1 -1
- package/templates/javascript/index.js +7 -5
- package/templates/javascript/src/config/index.config.js +5 -0
- package/templates/javascript/src/middlewares/index.middleware.js +5 -0
- package/templates/javascript/src/models/index.models.js +5 -0
- package/templates/javascript/src/routes/index.routes.js +1 -1
- package/templates/typescript/package.json +2 -1
- package/templates/typescript/src/config/index.config.ts +5 -0
- package/templates/typescript/src/controllers/index.controller.ts +11 -0
- package/templates/typescript/src/index.ts +13 -19
- package/templates/typescript/src/middlewares/errorHandler.ts +9 -0
- package/templates/typescript/src/middlewares/index.middleware.ts +5 -0
- package/templates/typescript/src/models/index.models.ts +5 -0
- package/templates/typescript/src/routes/index.route.ts +4 -3
- package/templates/typescript/src/routes/user.routes.ts +11 -0
- package/createPackageJsonFile.js +0 -60
- package/templates/typescript/src/controllers/test.controller.ts +0 -0
- /package/templates/javascript/src/routes/{uesr.routes.js → user.routes.js} +0 -0
- /package/templates/typescript/src/{utils → config}/db.config.ts +0 -0
package/README.md
CHANGED
package/bin/cli.js
CHANGED
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(`
|
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,18 +1,20 @@
|
|
1
1
|
import express from "express"
|
2
|
-
import
|
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
|
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,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
|
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.
|
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
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
})
|
28
|
-
|
29
|
-
|
30
|
-
|
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
|
+
})
|
@@ -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('/',
|
6
|
-
|
7
|
-
});
|
7
|
+
router.get('/', test);
|
8
|
+
router.use("/users", userRouter)
|
8
9
|
|
9
10
|
export default router;
|
package/createPackageJsonFile.js
DELETED
@@ -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
|
-
}
|
File without changes
|
File without changes
|
File without changes
|