-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
84 lines (71 loc) · 2.76 KB
/
app.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const express = require('express')
const cors = require('cors')
const fs = require('fs')
const path = require('path')
const port = 3000
require('dotenv').config()
//////////////////////////////////////////////////////////////////////////////////
const app = express()
const server = require("http").Server(app)
const io = require("socket.io")(server)
app.use(cors())
///////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////Always add these///////////////////////////////
app.set('view engine', 'html');
app.engine('html', require('ejs').renderFile)
app.use(express.json({limit: '50mb'}));
app.use(express.urlencoded({limit: '50mb'}));
app.use(express.static(__dirname+'/public/'));
app.use('/assets', express.static(path.join(__dirname, 'assets')))
// app.use('/models', express.static(path.join(__dirname, '/public/models')))
// app.use('/js', express.static(path.join(__dirname, 'js')))
// app.use('/views', express.static(path.join(__dirname, '/public/views')))
app.get('/',(req,res) => {
res.render('index1.html')
});
// Registration Form submit handling
app.get("/register", (req, res) => {
res.render('reg.html')
})
app.get("/print", (req, res) => {
res.render('print.html')
})
app.post('/addUser', (req,res) => {
const data = req.body;
// console.log(data)
const imageData = data.image64.replace(/^data:image\/\w+;base64,/, '');
// console.log(imageData)
const buffer = Buffer.from(imageData, 'base64');
// console.log(buffer)
fs.mkdirSync(`./assets/images/labels/${data.name}`)
const imgName = `./assets/images/labels/${data.name}/1.png`
fs.writeFileSync(imgName, buffer, err => {
if (err) { res.status(500).send({ error: 'Error saving image' })}
else { res.send({ 'ImageName':imgName })
console.log('Added New User!')}
});
})
app.get('/getLabels', (req,res)=>{
const testFolder = path.join(__dirname, './assets/images/labels');
// const fs = require('fs');
// console.log(path.join(__dirname, ''))
let labels = [];
fs.readdirSync(testFolder).forEach(file => {
// console.log(file);
labels.push(file)
});
// console.log(labels)
res.send(labels)
})
app.post("/upload", (req, res) => {
const data = req.body;
const imageData = data.image64.replace(/^data:image\/\w+;base64,/, '');
// console.log(imageData)
const buffer = Buffer.from(imageData, 'base64');
const imgName = `./uploads/image${Date.now()}.png`
fs.writeFile(imgName, buffer, err => {
if (err) { res.status(500).send({ error: 'Error saving image' })}
else { res.send({ 'ImageName':imgName })}
});
});
server.listen(port, ()=>console.log(`App started running on port: ${port}`))