-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (53 loc) · 1.79 KB
/
index.js
File metadata and controls
66 lines (53 loc) · 1.79 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import path from "path";
import { fileURLToPath } from "url";
import express from "express";
import multer from "multer";
import ImageClassification from "@nanonets/image-classification";
import dotenv from "dotenv";
// Server initialization
const app = express();
const upload = multer({ dest: "public/uploads" });
dotenv.config();
// Make "__dirname" available in ES module
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Static files
app.use("/public", express.static(__dirname + "/public"));
// Body Parser
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
// Class instantiation
const ic = new ImageClassification(
process.env.API_KEY_1,
process.env.MODEL_ID_1
);
// Routes
app.get("/", async (req, res) => {
// const icUrlArray = [process.env.FILE_URL_1, process.env.FILE_URL_2];
// console.log(
// await ic.getModelDetails(),
// "\n-------------------------------------------------------\n"
// );
// console.log(
// await ic.predictUsingUrls(icUrlArray),
// "\n-------------------------------------------------------\n"
// );
res.sendFile(__dirname + "/public/index.html");
});
app.get("/uploadFile", (req, res) => {
res.sendFile(__dirname + "/public/fileUpload.html");
});
app.post("/uploadFile", upload.single("file"), async (req, res) => {
console.log("Uploaded file details: ", req.file, "\n\nResponse:");
// console.log(
// await ic.predictUsingFile(req.file.path),
// "\n-------------------------------------------------------\n"
// );
res.send("Uploaded. Check the server console for the response.");
});
// Server port
const PORT = process.env.PORT || 5000;
app.listen(PORT, (err) => {
if (err) console.error("Node.js server error: ", err);
else console.log(`Server started on port ${PORT}...`);
});