forked from irenfonseca/predict
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
44 lines (33 loc) · 1.13 KB
/
Copy pathserver.js
File metadata and controls
44 lines (33 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// server.js
require("dotenv").config();
const express = require("express");
const path = require("path");
const mongoose = require("mongoose"); // NUEVO: Importar mongoose
const predictRoutes = require("./routes/predictRoutes");
const { initModel } = require("./services/tfModelService");
const PORT = process.env.PORT || 3002;
const MONGO_URI = process.env.MONGO_URI || "mongodb://localhost:27017/prediction";
const app = express();
app.use(express.json());
// Servir la carpeta del modelo TFJS
const modelDir = path.resolve(__dirname, "model");
app.use("/model", express.static(modelDir));
// Rutas
app.use("/", predictRoutes);
app.listen(PORT, async () => {
const serverUrl = `http://localhost:${PORT}`;
console.log(`[PREDICT] Servicio escuchando en ${serverUrl}`);
try {
await mongoose.connect(MONGO_URI);
console.log("[PREDICT] Conectado a MongoDB correctamente");
} catch (err) {
console.error("[PREDICT] Error conectando a MongoDB:", err);
}
// Carga del modelo IA
try {
await initModel(serverUrl);
} catch (err) {
console.error("Error al inicializar modelo:", err);
process.exit(1);
}
});