apiforge-cli 1.0.1
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 +10 -0
- package/bin/apiforge.js +19 -0
- package/lib/create.js +24 -0
- package/package.json +16 -0
- package/templates/auth/index.js +67 -0
- package/templates/auth/package.json +11 -0
- package/templates/cors/index.js +18 -0
- package/templates/cors/package.json +10 -0
- package/templates/crud/index.js +37 -0
- package/templates/crud/package.json +9 -0
- package/templates/firebase/index.js +15 -0
- package/templates/firebase/package.json +10 -0
- package/templates/graphql/index.js +32 -0
- package/templates/graphql/package.json +11 -0
- package/templates/jwt/index.js +33 -0
- package/templates/jwt/package.json +10 -0
- package/templates/mongodb/index.js +26 -0
- package/templates/mongodb/package.json +10 -0
- package/templates/mysql/index.js +27 -0
- package/templates/mysql/package.json +10 -0
- package/templates/netlify/netlify/functions/index.js +14 -0
- package/templates/netlify/package.json +7 -0
- package/templates/oauth2/index.js +28 -0
- package/templates/oauth2/package.json +11 -0
- package/templates/postgres/index.js +30 -0
- package/templates/postgres/package.json +10 -0
- package/templates/ratelimit/index.js +24 -0
- package/templates/ratelimit/package.json +10 -0
- package/templates/receiver/index.js +30 -0
- package/templates/receiver/package.json +9 -0
- package/templates/rest/index.js +22 -0
- package/templates/rest/package.json +9 -0
- package/templates/sqlite/index.js +23 -0
- package/templates/sqlite/package.json +10 -0
- package/templates/swagger/index.js +33 -0
- package/templates/swagger/package.json +11 -0
- package/templates/typescript/index.ts +16 -0
- package/templates/typescript/package.json +14 -0
- package/templates/upload/index.js +30 -0
- package/templates/upload/package.json +10 -0
- package/templates/vercel/api/index.js +8 -0
- package/templates/vercel/package.json +9 -0
- package/templates/websocket/index.js +28 -0
- package/templates/websocket/package.json +10 -0
- package/templates/workers/index.js +17 -0
- package/templates/workers/package.json +7 -0
package/README.md
ADDED
package/bin/apiforge.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { createProject } from "../lib/create.js";
|
|
4
|
+
|
|
5
|
+
const args = process.argv.slice(2);
|
|
6
|
+
|
|
7
|
+
if (args[0] === "-create") {
|
|
8
|
+
const type = args[1];
|
|
9
|
+
const name = args[2];
|
|
10
|
+
|
|
11
|
+
if (!type || !name) {
|
|
12
|
+
console.log("Uso: apiforge -create <tipo> <nombre>");
|
|
13
|
+
process.exit(1);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
createProject(type, name);
|
|
17
|
+
} else {
|
|
18
|
+
console.log("Comando no reconocido");
|
|
19
|
+
}
|
package/lib/create.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import fs from "fs-extra";
|
|
2
|
+
import path from "path";
|
|
3
|
+
|
|
4
|
+
export function createProject(type, name) {
|
|
5
|
+
const template = path.join(
|
|
6
|
+
process.cwd(),
|
|
7
|
+
"templates",
|
|
8
|
+
type
|
|
9
|
+
);
|
|
10
|
+
|
|
11
|
+
const destination = path.join(
|
|
12
|
+
process.cwd(),
|
|
13
|
+
name
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
if (!fs.existsSync(template)) {
|
|
17
|
+
console.log("Plantilla no encontrada");
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
fs.copySync(template, destination);
|
|
22
|
+
|
|
23
|
+
console.log(`Proyecto creado: ${name}`);
|
|
24
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "apiforge-cli",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "CLI for automatically creating APIs",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"apiforge": "./bin/apiforge.js"
|
|
8
|
+
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"chalk": "^5.0.0",
|
|
12
|
+
"commander": "^12.0.0",
|
|
13
|
+
"fs-extra": "^11.0.0"
|
|
14
|
+
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import jwt from "jsonwebtoken";
|
|
3
|
+
import bcrypt from "bcrypt";
|
|
4
|
+
|
|
5
|
+
const app = express();
|
|
6
|
+
|
|
7
|
+
app.use(express.json());
|
|
8
|
+
|
|
9
|
+
const SECRET = "apiforge-secret";
|
|
10
|
+
|
|
11
|
+
const users = [];
|
|
12
|
+
|
|
13
|
+
app.post("/register", async (req, res) => {
|
|
14
|
+
|
|
15
|
+
const password = await bcrypt.hash(
|
|
16
|
+
req.body.password,
|
|
17
|
+
10
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
users.push({
|
|
21
|
+
username: req.body.username,
|
|
22
|
+
password,
|
|
23
|
+
role: "user"
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
res.json({
|
|
27
|
+
created: true
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
app.post("/login", async (req, res) => {
|
|
33
|
+
|
|
34
|
+
const user = users.find(
|
|
35
|
+
u => u.username === req.body.username
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
if (!user) {
|
|
39
|
+
return res.status(404).json({
|
|
40
|
+
error: "User not found"
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const token = jwt.sign(
|
|
45
|
+
{
|
|
46
|
+
username: user.username,
|
|
47
|
+
role: user.role
|
|
48
|
+
},
|
|
49
|
+
SECRET
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
res.json({
|
|
53
|
+
token
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
app.get("/", (req, res) => {
|
|
59
|
+
res.json({
|
|
60
|
+
api: "Auth API",
|
|
61
|
+
status: "online"
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
app.listen(3000, () => {
|
|
66
|
+
console.log("Auth API funcionando");
|
|
67
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import cors from "cors";
|
|
3
|
+
|
|
4
|
+
const app = express();
|
|
5
|
+
|
|
6
|
+
app.use(cors());
|
|
7
|
+
app.use(express.json());
|
|
8
|
+
|
|
9
|
+
app.get("/", (req, res) => {
|
|
10
|
+
res.json({
|
|
11
|
+
api: "CORS API",
|
|
12
|
+
status: "online"
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
app.listen(3000, () => {
|
|
17
|
+
console.log("CORS API funcionando");
|
|
18
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
|
|
3
|
+
const app = express();
|
|
4
|
+
|
|
5
|
+
app.use(express.json());
|
|
6
|
+
|
|
7
|
+
let users = [];
|
|
8
|
+
|
|
9
|
+
app.get("/users", (req, res) => {
|
|
10
|
+
res.json(users);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
app.post("/users", (req, res) => {
|
|
14
|
+
const user = req.body;
|
|
15
|
+
users.push(user);
|
|
16
|
+
|
|
17
|
+
res.json({
|
|
18
|
+
created: true,
|
|
19
|
+
user
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
app.put("/users/:id", (req, res) => {
|
|
24
|
+
users[req.params.id] = req.body;
|
|
25
|
+
|
|
26
|
+
res.json({
|
|
27
|
+
updated: true
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
app.delete("/users/:id", (req, res) => {
|
|
32
|
+
users.splice(req.params.id, 1);
|
|
33
|
+
|
|
34
|
+
res.json({
|
|
35
|
+
deleted: true
|
|
36
|
+
});
|
|
37
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { onRequest } from "firebase-functions/v2/https";
|
|
2
|
+
import express from "express";
|
|
3
|
+
|
|
4
|
+
const app = express();
|
|
5
|
+
|
|
6
|
+
app.use(express.json());
|
|
7
|
+
|
|
8
|
+
app.get("/", (req, res) => {
|
|
9
|
+
res.json({
|
|
10
|
+
api: "Firebase Functions API",
|
|
11
|
+
status: "online"
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
export const api = onRequest(app);
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import { graphqlHTTP } from "express-graphql";
|
|
3
|
+
import {
|
|
4
|
+
buildSchema
|
|
5
|
+
} from "graphql";
|
|
6
|
+
|
|
7
|
+
const app = express();
|
|
8
|
+
|
|
9
|
+
const schema = buildSchema(`
|
|
10
|
+
type Query {
|
|
11
|
+
hello: String
|
|
12
|
+
}
|
|
13
|
+
`);
|
|
14
|
+
|
|
15
|
+
const root = {
|
|
16
|
+
hello() {
|
|
17
|
+
return "GraphQL API funcionando";
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
app.use(
|
|
22
|
+
"/graphql",
|
|
23
|
+
graphqlHTTP({
|
|
24
|
+
schema,
|
|
25
|
+
rootValue: root,
|
|
26
|
+
graphiql: true
|
|
27
|
+
})
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
app.listen(3000, () => {
|
|
31
|
+
console.log("GraphQL API lista");
|
|
32
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import jwt from "jsonwebtoken";
|
|
3
|
+
|
|
4
|
+
const app = express();
|
|
5
|
+
|
|
6
|
+
app.use(express.json());
|
|
7
|
+
|
|
8
|
+
const SECRET = "apiforge-secret";
|
|
9
|
+
|
|
10
|
+
app.post("/login", (req, res) => {
|
|
11
|
+
const { user } = req.body;
|
|
12
|
+
|
|
13
|
+
const token = jwt.sign(
|
|
14
|
+
{ user },
|
|
15
|
+
SECRET,
|
|
16
|
+
{ expiresIn: "1h" }
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
res.json({
|
|
20
|
+
token
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
app.get("/", (req, res) => {
|
|
25
|
+
res.json({
|
|
26
|
+
api: "JWT API",
|
|
27
|
+
status: "online"
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
app.listen(3000, () => {
|
|
32
|
+
console.log("JWT API funcionando");
|
|
33
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import mongoose from "mongoose";
|
|
3
|
+
|
|
4
|
+
const app = express();
|
|
5
|
+
|
|
6
|
+
app.use(express.json());
|
|
7
|
+
|
|
8
|
+
mongoose.connect("mongodb://127.0.0.1:27017/apiforge");
|
|
9
|
+
|
|
10
|
+
const User = mongoose.model(
|
|
11
|
+
"User",
|
|
12
|
+
new mongoose.Schema({
|
|
13
|
+
name: String
|
|
14
|
+
})
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
app.get("/", (req, res) => {
|
|
18
|
+
res.json({
|
|
19
|
+
api: "MongoDB API",
|
|
20
|
+
status: "online"
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
app.listen(3000, () => {
|
|
25
|
+
console.log("MongoDB API funcionando");
|
|
26
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import mysql from "mysql2/promise";
|
|
3
|
+
|
|
4
|
+
const app = express();
|
|
5
|
+
|
|
6
|
+
app.use(express.json());
|
|
7
|
+
|
|
8
|
+
const db = await mysql.createConnection({
|
|
9
|
+
host: "localhost",
|
|
10
|
+
user: "root",
|
|
11
|
+
password: "",
|
|
12
|
+
database: "apiforge"
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
app.get("/", async (req, res) => {
|
|
16
|
+
const [rows] = await db.query("SELECT NOW() AS serverTime");
|
|
17
|
+
|
|
18
|
+
res.json({
|
|
19
|
+
api: "MySQL API",
|
|
20
|
+
status: "online",
|
|
21
|
+
serverTime: rows[0].serverTime
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
app.listen(3000, () => {
|
|
26
|
+
console.log("MySQL API funcionando");
|
|
27
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import passport from "passport";
|
|
3
|
+
import { Strategy as OAuth2Strategy } from "passport-oauth2";
|
|
4
|
+
|
|
5
|
+
const app = express();
|
|
6
|
+
|
|
7
|
+
passport.use(new OAuth2Strategy({
|
|
8
|
+
authorizationURL: "https://example.com/oauth/authorize",
|
|
9
|
+
tokenURL: "https://example.com/oauth/token",
|
|
10
|
+
clientID: "CLIENT_ID",
|
|
11
|
+
clientSecret: "CLIENT_SECRET",
|
|
12
|
+
callbackURL: "/auth/callback"
|
|
13
|
+
}, (accessToken, refreshToken, profile, done) => {
|
|
14
|
+
return done(null, { accessToken });
|
|
15
|
+
}));
|
|
16
|
+
|
|
17
|
+
app.use(passport.initialize());
|
|
18
|
+
|
|
19
|
+
app.get("/", (req, res) => {
|
|
20
|
+
res.json({
|
|
21
|
+
api: "OAuth2 API",
|
|
22
|
+
status: "online"
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
app.listen(3000, () => {
|
|
27
|
+
console.log("OAuth2 API funcionando");
|
|
28
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import { Client } from "pg";
|
|
3
|
+
|
|
4
|
+
const app = express();
|
|
5
|
+
|
|
6
|
+
app.use(express.json());
|
|
7
|
+
|
|
8
|
+
const db = new Client({
|
|
9
|
+
host: "localhost",
|
|
10
|
+
port: 5432,
|
|
11
|
+
user: "postgres",
|
|
12
|
+
password: "password",
|
|
13
|
+
database: "apiforge"
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
await db.connect();
|
|
17
|
+
|
|
18
|
+
app.get("/", async (req, res) => {
|
|
19
|
+
const result = await db.query("SELECT NOW()");
|
|
20
|
+
|
|
21
|
+
res.json({
|
|
22
|
+
api: "PostgreSQL API",
|
|
23
|
+
status: "online",
|
|
24
|
+
serverTime: result.rows[0].now
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
app.listen(3000, () => {
|
|
29
|
+
console.log("PostgreSQL API funcionando");
|
|
30
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import rateLimit from "express-rate-limit";
|
|
3
|
+
|
|
4
|
+
const app = express();
|
|
5
|
+
|
|
6
|
+
app.use(express.json());
|
|
7
|
+
|
|
8
|
+
const limiter = rateLimit({
|
|
9
|
+
windowMs: 60 * 1000,
|
|
10
|
+
max: 10
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
app.use(limiter);
|
|
14
|
+
|
|
15
|
+
app.get("/", (req, res) => {
|
|
16
|
+
res.json({
|
|
17
|
+
api: "Rate Limit API",
|
|
18
|
+
status: "online"
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
app.listen(3000, () => {
|
|
23
|
+
console.log("Rate Limit API funcionando");
|
|
24
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
|
|
3
|
+
const app = express();
|
|
4
|
+
|
|
5
|
+
app.use(express.json());
|
|
6
|
+
|
|
7
|
+
app.post("/api/data", (req, res) => {
|
|
8
|
+
|
|
9
|
+
console.log("Datos recibidos:");
|
|
10
|
+
console.log(req.body);
|
|
11
|
+
|
|
12
|
+
res.json({
|
|
13
|
+
success: true,
|
|
14
|
+
received: req.body
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
app.get("/", (req, res) => {
|
|
20
|
+
|
|
21
|
+
res.json({
|
|
22
|
+
api: "HTTP Receiver",
|
|
23
|
+
status: "online"
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
app.listen(3000, () => {
|
|
29
|
+
console.log("Receiver API en puerto 3000");
|
|
30
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
|
|
3
|
+
const app = express();
|
|
4
|
+
|
|
5
|
+
app.use(express.json());
|
|
6
|
+
|
|
7
|
+
app.get("/", (req, res) => {
|
|
8
|
+
res.json({
|
|
9
|
+
api: "REST API",
|
|
10
|
+
status: "online"
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
app.post("/api/data", (req, res) => {
|
|
15
|
+
res.json({
|
|
16
|
+
received: req.body
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
app.listen(3000, () => {
|
|
21
|
+
console.log("REST API on port 3000");
|
|
22
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import sqlite3 from "sqlite3";
|
|
3
|
+
|
|
4
|
+
const app = express();
|
|
5
|
+
|
|
6
|
+
app.use(express.json());
|
|
7
|
+
|
|
8
|
+
const db = new sqlite3.Database("database.db");
|
|
9
|
+
|
|
10
|
+
db.serialize(() => {
|
|
11
|
+
db.run("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)");
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
app.get("/", (req, res) => {
|
|
15
|
+
res.json({
|
|
16
|
+
api: "SQLite API",
|
|
17
|
+
status: "online"
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
app.listen(3000, () => {
|
|
22
|
+
console.log("SQLite API funcionando");
|
|
23
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import swaggerUi from "swagger-ui-express";
|
|
3
|
+
import swaggerJsdoc from "swagger-jsdoc";
|
|
4
|
+
|
|
5
|
+
const app = express();
|
|
6
|
+
|
|
7
|
+
const swaggerSpec = swaggerJsdoc({
|
|
8
|
+
definition: {
|
|
9
|
+
openapi: "3.0.0",
|
|
10
|
+
info: {
|
|
11
|
+
title: "APIForge API",
|
|
12
|
+
version: "1.0.0"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
apis: []
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
app.use(
|
|
19
|
+
"/docs",
|
|
20
|
+
swaggerUi.serve,
|
|
21
|
+
swaggerUi.setup(swaggerSpec)
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
app.get("/", (req, res) => {
|
|
25
|
+
res.json({
|
|
26
|
+
api: "Swagger API",
|
|
27
|
+
status: "online"
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
app.listen(3000, () => {
|
|
32
|
+
console.log("Swagger API funcionando");
|
|
33
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
|
|
3
|
+
const app = express();
|
|
4
|
+
|
|
5
|
+
app.use(express.json());
|
|
6
|
+
|
|
7
|
+
app.get("/", (req, res) => {
|
|
8
|
+
res.json({
|
|
9
|
+
api: "TypeScript API",
|
|
10
|
+
status: "online"
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
app.listen(3000, () => {
|
|
15
|
+
console.log("TypeScript API funcionando");
|
|
16
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "typescript-api",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"express": "^5.0.0"
|
|
8
|
+
},
|
|
9
|
+
"devDependencies": {
|
|
10
|
+
"typescript": "^5.0.0",
|
|
11
|
+
"tsx": "^4.0.0",
|
|
12
|
+
"@types/express": "^5.0.0"
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import multer from "multer";
|
|
3
|
+
|
|
4
|
+
const app = express();
|
|
5
|
+
|
|
6
|
+
const upload = multer({
|
|
7
|
+
dest: "uploads/"
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
app.post("/upload", upload.single("file"), (req, res) => {
|
|
11
|
+
|
|
12
|
+
res.json({
|
|
13
|
+
success: true,
|
|
14
|
+
file: req.file
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
app.get("/", (req, res) => {
|
|
20
|
+
|
|
21
|
+
res.json({
|
|
22
|
+
api: "Upload API",
|
|
23
|
+
status: "online"
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
app.listen(3000, () => {
|
|
29
|
+
console.log("Upload API funcionando");
|
|
30
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import { WebSocketServer } from "ws";
|
|
3
|
+
|
|
4
|
+
const app = express();
|
|
5
|
+
|
|
6
|
+
const server = app.listen(3000, () => {
|
|
7
|
+
console.log("WebSocket API en puerto 3000");
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
const wss = new WebSocketServer({
|
|
11
|
+
server
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
wss.on("connection", (socket) => {
|
|
15
|
+
|
|
16
|
+
socket.send("Conectado al WebSocket");
|
|
17
|
+
|
|
18
|
+
socket.on("message", (message) => {
|
|
19
|
+
|
|
20
|
+
console.log("Mensaje:", message.toString());
|
|
21
|
+
|
|
22
|
+
socket.send(
|
|
23
|
+
"Recibido: " + message
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
});
|