4bnode 3.5.2 → 4.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/README.md +139 -114
- package/add-api.js +288 -156
- package/add-crud.js +226 -0
- package/add-login.js +152 -319
- package/add-mongo-delete.js +67 -138
- package/add-mongo-insert.js +87 -204
- package/add-mongo-read.js +100 -216
- package/add-mongo-schema.js +431 -199
- package/add-mongo-update.js +89 -202
- package/add-mongo.js +125 -218
- package/add-serialport.js +53 -98
- package/add-socket.js +43 -66
- package/add-websocket.js +68 -140
- package/index.js +288 -155
- package/lib/env.js +18 -0
- package/lib/indexFile.js +87 -0
- package/lib/names.js +21 -0
- package/lib/project.js +122 -0
- package/lib/routeFile.js +56 -0
- package/lib/ui.js +161 -0
- package/package.json +9 -16
- package/skeleton/dev-api.js +2785 -0
- package/skeleton/index.js +10 -3
- package/skeleton/package.json +9 -5
- package/skeleton/public/fonts/NotoSans-Bold.woff2 +0 -0
- package/skeleton/public/fonts/NotoSans-Light.woff2 +0 -0
- package/skeleton/public/fonts/NotoSans-Medium.woff2 +0 -0
- package/skeleton/public/fonts/NotoSans-Regular.woff2 +0 -0
- package/skeleton/public/fonts/NotoSans-SemiBold.woff2 +0 -0
- package/skeleton/public/images/mongo.png +0 -0
- package/skeleton/public/images/mysql.png +0 -0
- package/skeleton/public/images/postgresql.png +0 -0
- package/skeleton/public/images/sqlite.png +0 -0
- package/skeleton/public/index.html +7562 -64
- package/README_4bnode.md +0 -197
- package/src/auth/passport.js +0 -21
- package/src/db.js +0 -13
- package/test-server/.env.development +0 -1
- package/test-server/.env.production +0 -1
- package/test-server/index.js +0 -82
- package/test-server/package-lock.json +0 -1267
- package/test-server/package.json +0 -20
- package/test-server/public/index.html +0 -90
package/README_4bnode.md
DELETED
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
# 4Brains Node Generator
|
|
2
|
-
|
|
3
|
-
A comprehensive CLI tool to streamline the generation and management of Node.js applications by 4Brains Technologies.
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
To install the 4Brains Node Generator globally, use the following command:
|
|
8
|
-
|
|
9
|
-
```sh
|
|
10
|
-
npm install -g 4brains-node-generator
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
## Available Commands
|
|
14
|
-
|
|
15
|
-
### `4bnode init <projectName>`
|
|
16
|
-
|
|
17
|
-
Initialize a new Node.js project with the specified name.
|
|
18
|
-
|
|
19
|
-
```sh
|
|
20
|
-
4bnode init my-project
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
### `4bnode add-api <endpointName>`
|
|
24
|
-
|
|
25
|
-
Add a new API endpoint to your project.
|
|
26
|
-
|
|
27
|
-
```sh
|
|
28
|
-
4bnode add-api users
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
### `4bnode add-mongo`
|
|
32
|
-
|
|
33
|
-
Integrate MongoDB into your project. This command sets up the necessary MongoDB configuration.
|
|
34
|
-
|
|
35
|
-
```sh
|
|
36
|
-
4bnode add-mongo
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
### `4bnode add-mongo-schema <schemaName>`
|
|
40
|
-
|
|
41
|
-
Create a new MongoDB schema. You will be prompted to enter field details for the schema.
|
|
42
|
-
|
|
43
|
-
```sh
|
|
44
|
-
4bnode add-mongo-schema User
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
### `4bnode add-login`
|
|
48
|
-
|
|
49
|
-
Add a login template to an existing route in your project. You will be prompted to select a route and a model, and then enter the login fields.
|
|
50
|
-
|
|
51
|
-
```sh
|
|
52
|
-
4bnode add-login
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
### `4bnode add-mongo-insert`
|
|
56
|
-
|
|
57
|
-
Add a template for inserting data into a MongoDB collection. This command allows you to easily create routes for data insertion.
|
|
58
|
-
|
|
59
|
-
```sh
|
|
60
|
-
4bnode add-mongo-insert
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
### `4bnode add-mongo-update`
|
|
64
|
-
|
|
65
|
-
Add a template for updating data in a MongoDB collection. This facilitates creating routes for updating documents.
|
|
66
|
-
|
|
67
|
-
```sh
|
|
68
|
-
4bnode add-mongo-update
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
### `4bnode add-mongo-read`
|
|
72
|
-
|
|
73
|
-
Add a template for reading data from a MongoDB collection, helping you to quickly set up read operations.
|
|
74
|
-
|
|
75
|
-
```sh
|
|
76
|
-
4bnode add-mongo-read
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
### `4bnode add-mongo-delete`
|
|
80
|
-
|
|
81
|
-
Add a template for deleting data from a MongoDB collection, making it easy to handle deletion operations.
|
|
82
|
-
|
|
83
|
-
```sh
|
|
84
|
-
4bnode add-mongo-delete
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
### `4bnode add-socket`
|
|
88
|
-
|
|
89
|
-
Integrate Socket.io into your project for real-time communication capabilities.
|
|
90
|
-
|
|
91
|
-
```sh
|
|
92
|
-
4bnode add-socket
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
### `4bnode add-websocket`
|
|
96
|
-
|
|
97
|
-
Add native WebSocket support to your project.
|
|
98
|
-
|
|
99
|
-
```sh
|
|
100
|
-
4bnode add-websocket
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
### `4bnode add-serialport`
|
|
104
|
-
|
|
105
|
-
Integrate SerialPort functionality into your project. This command does the following:
|
|
106
|
-
- Prompts for Serial Port path and baud rate
|
|
107
|
-
- Automatically updates `.env.development` and `.env.production` with `SERIALPORT_PATH` and `BAUDRATE`
|
|
108
|
-
- Adds SerialPort and Readline parser setup to your `index.js`
|
|
109
|
-
- Installs required dependencies: `serialport` and `@serialport/parser-readline`
|
|
110
|
-
|
|
111
|
-
```sh
|
|
112
|
-
4bnode add-serialport
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
Once added, your project will automatically initialize the serial port with the specified configuration and log incoming data.
|
|
116
|
-
|
|
117
|
-
Example incoming data log:
|
|
118
|
-
```sh
|
|
119
|
-
Received data: <data from serial device>
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
## Examples
|
|
123
|
-
|
|
124
|
-
### Adding MongoDB Schema
|
|
125
|
-
|
|
126
|
-
When you run the command to add a MongoDB schema, you will be prompted to enter the field details.
|
|
127
|
-
|
|
128
|
-
```sh
|
|
129
|
-
4bnode add-mongo-schema User
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
### Adding a Login Template
|
|
133
|
-
|
|
134
|
-
When you run the command to add a login template, you will be prompted to select a route and a model, and then enter the login fields.
|
|
135
|
-
|
|
136
|
-
```sh
|
|
137
|
-
4bnode add-login
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
## Real-time Communication Integration
|
|
141
|
-
|
|
142
|
-
### WebSocket Integration
|
|
143
|
-
|
|
144
|
-
When you add WebSocket integration, it will automatically be added to your project.
|
|
145
|
-
|
|
146
|
-
```sh
|
|
147
|
-
4bnode add-websocket
|
|
148
|
-
```
|
|
149
|
-
|
|
150
|
-
To use `req.wss` in your route endpoints, you can access the WebSocket server as follows:
|
|
151
|
-
|
|
152
|
-
```js
|
|
153
|
-
router.get("/some-endpoint", (req, res) => {
|
|
154
|
-
req.wss.clients.forEach((client) => {
|
|
155
|
-
if (client.readyState === WebSocket.OPEN) {
|
|
156
|
-
client.send("Hello, client!");
|
|
157
|
-
}
|
|
158
|
-
});
|
|
159
|
-
res.send("Message sent to all WebSocket clients.");
|
|
160
|
-
});
|
|
161
|
-
```
|
|
162
|
-
|
|
163
|
-
### Socket.io Integration
|
|
164
|
-
|
|
165
|
-
When you add Socket.io integration, it will automatically be added to your project.
|
|
166
|
-
|
|
167
|
-
```sh
|
|
168
|
-
4bnode add-socket
|
|
169
|
-
```
|
|
170
|
-
|
|
171
|
-
To use `req.io` in your route endpoints, you can access the Socket.io server as follows:
|
|
172
|
-
|
|
173
|
-
```js
|
|
174
|
-
router.get("/some-endpoint", (req, res) => {
|
|
175
|
-
req.io.emit("message", "Hello, Socket.io clients!");
|
|
176
|
-
res.send("Message sent to all Socket.io clients.");
|
|
177
|
-
});
|
|
178
|
-
```
|
|
179
|
-
|
|
180
|
-
## Help
|
|
181
|
-
|
|
182
|
-
If you enter an invalid command, you will see the following message:
|
|
183
|
-
|
|
184
|
-
```sh
|
|
185
|
-
Unknown command. Use -h or --help for help.
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
You can also use `-h` or `--help` to get help information.
|
|
189
|
-
|
|
190
|
-
```sh
|
|
191
|
-
4bnode -h
|
|
192
|
-
4bnode --help
|
|
193
|
-
```
|
|
194
|
-
|
|
195
|
-
## License
|
|
196
|
-
|
|
197
|
-
This project is licensed under the ISC License.
|
package/src/auth/passport.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import passport from 'passport';
|
|
2
|
-
import { Strategy as JwtStrategy, ExtractJwt } from 'passport-jwt';
|
|
3
|
-
import mongoose from 'mongoose';
|
|
4
|
-
import User from '../models/User.js'; // Adjust the path to your user model
|
|
5
|
-
|
|
6
|
-
const opts = {};
|
|
7
|
-
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
|
|
8
|
-
opts.secretOrKey = process.env.JWT_SECRET;
|
|
9
|
-
|
|
10
|
-
passport.use(new JwtStrategy(opts, (jwt_payload, done) => {
|
|
11
|
-
User.findById(jwt_payload.id)
|
|
12
|
-
.then(user => {
|
|
13
|
-
if (user) {
|
|
14
|
-
return done(null, user);
|
|
15
|
-
}
|
|
16
|
-
return done(null, false);
|
|
17
|
-
})
|
|
18
|
-
.catch(err => console.error(err));
|
|
19
|
-
}));
|
|
20
|
-
|
|
21
|
-
export default passport;
|
package/src/db.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import mongoose from 'mongoose';
|
|
2
|
-
|
|
3
|
-
const connectDB = async () => {
|
|
4
|
-
try {
|
|
5
|
-
const conn = await mongoose.connect(process.env.MONGO_URI);
|
|
6
|
-
console.log(`MongoDB connected: ${conn.connection.host}`);
|
|
7
|
-
} catch (err) {
|
|
8
|
-
console.error(err);
|
|
9
|
-
process.exit(1);
|
|
10
|
-
}
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export default connectDB;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
PORT=3000
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
PORT=3000
|
package/test-server/index.js
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { WebSocketServer } from 'ws';
|
|
2
|
-
import http from 'http';
|
|
3
|
-
import express from "express";
|
|
4
|
-
import dotenv from "dotenv";
|
|
5
|
-
import bodyParser from "body-parser";
|
|
6
|
-
import path from "path";
|
|
7
|
-
import cors from "cors";
|
|
8
|
-
import os from "os";
|
|
9
|
-
|
|
10
|
-
const env = process.env.NODE_ENV || "development";
|
|
11
|
-
dotenv.config({ path: `.env.${env}` });
|
|
12
|
-
|
|
13
|
-
const app = express();
|
|
14
|
-
const port = process.env.PORT || 3000;
|
|
15
|
-
|
|
16
|
-
// Middleware
|
|
17
|
-
|
|
18
|
-
const server = http.createServer(app);
|
|
19
|
-
const wsServer = new WebSocketServer({ server });
|
|
20
|
-
console.log(`WebSocket server is running on the same port as the server (${port})`);
|
|
21
|
-
|
|
22
|
-
wsServer.on('connection', (ws) => {
|
|
23
|
-
console.log('A client connected');
|
|
24
|
-
|
|
25
|
-
ws.on('message', (message) => {
|
|
26
|
-
try {
|
|
27
|
-
const parsedMessage = JSON.parse(message);
|
|
28
|
-
console.log('Received:', parsedMessage);
|
|
29
|
-
|
|
30
|
-
// Broadcast to all clients except sender
|
|
31
|
-
wsServer.clients.forEach((client) => {
|
|
32
|
-
if (client !== ws && client.readyState === ws.OPEN) {
|
|
33
|
-
client.send(JSON.stringify(parsedMessage));
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
} catch (error) {
|
|
37
|
-
console.error('Invalid message format:', error);
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
ws.on('close', () => {
|
|
42
|
-
console.log('Client disconnected');
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
app.use((req, res, next) => {
|
|
47
|
-
req.wss = wsServer;
|
|
48
|
-
next();
|
|
49
|
-
});
|
|
50
|
-
app.use(bodyParser.json({ limit: "50mb" }));
|
|
51
|
-
app.use(bodyParser.urlencoded({ extended: true, limit: "50mb" }));
|
|
52
|
-
app.use(cors({ origin: "*" }));
|
|
53
|
-
|
|
54
|
-
// Serve static files from the public directory
|
|
55
|
-
const __dirname = path.resolve();
|
|
56
|
-
app.use(express.static(path.join(__dirname, "public")));
|
|
57
|
-
|
|
58
|
-
// Get local network IP address
|
|
59
|
-
function getLocalNetworkIp() {
|
|
60
|
-
const interfaces = os.networkInterfaces();
|
|
61
|
-
for (const name of Object.keys(interfaces)) {
|
|
62
|
-
for (const iface of interfaces[name]) {
|
|
63
|
-
if (iface.family === "IPv4" && !iface.internal) {
|
|
64
|
-
return iface.address;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
return null;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// Start server
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
server.listen(port, () => {
|
|
75
|
-
const localIp = getLocalNetworkIp();
|
|
76
|
-
console.log(`App is running on http://localhost:${port}`);
|
|
77
|
-
if (localIp) {
|
|
78
|
-
console.log(
|
|
79
|
-
`App is also accessible on your local network at http://${localIp}:${port}`
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
});
|