-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
191 lines (154 loc) · 7.3 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const express = require("express");
const bodyParser = require("body-parser");
const multer = require('multer') // multer will be used to handle the form data.
const Aws = require('aws-sdk') // aws-sdk library will used to upload image to s3 bucket.
const path = require('path');
const app = express();
const port = 5000;
const mysql = require("mysql");
require('dotenv').config();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static('dist')); // it makes the client side able to get the index.html and main.js when url pattern is "/".
let con = mysql.createConnection({ // it makes connection to MySQL database possible
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE
});
con.connect(); // start the connection
const s3 = new Aws.S3({
accessKeyId: process.env.AWS_ACCESS_KEY_ID, // accessKeyId that is stored in .env file
secretAccessKey: process.env.AWS_ACCESS_KEY_SECRET // secretAccessKey is also store in .env file
})
app.get(['/board/:id', '/board/:id/create'], function (req, res) { // http request to url "/board/:id" and get index.html file
console.log("get file from the path: ", path.join(__dirname, "/dist/index.html"));
res.sendFile(path.join(__dirname, "/dist/index.html"));
});
app.get('/get-board/:boardId', function (req, res) {
let { boardId } = req.params;
con.query("SELECT CardID, Msgs, CreateTime, SenderLastName, SenderFirstName, ImageURL FROM Cards WHERE BoardID = ?", [[[boardId]]], (err, result, fields) => {
if (err) throw err;
console.log(result);
console.log("These are cards' data.");
res.json({ BoardContent: result.map(obj => { return { CardID: obj.CardID, Msgs: obj.Msgs, CreateTime: obj.CreateTime, SenderLastName: obj.SenderLastName, SenderFirstName: obj.SenderFirstName, ImageURL: obj.ImageURL } }) })
})
});
// creating the storage variable to upload the file and providing the destination folder,
// if nothing is provided in the callback it will get uploaded in main directory
const storage = multer.memoryStorage({
destination: function (req, file, cb) {
cb(null, '')
}
})
// below variable is define to check the type of file which is uploaded
const filefilter = (req, file, cb) => {
if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/jpg') {
cb(null, true)
} else {
cb(null, false)
}
}
// defining the upload variable for the configuration of photo being uploaded
const upload = multer({
storage: storage,
limits: { fileSize: 1000000 },
fileFilter: filefilter
});
app.post('/add-card', upload.single('cardImage'), (req, res) => {
// Definning the params variable to uplaod the photo
const params = {
Bucket: process.env.AWS_BUCKET_NAME, // bucket that we made earlier
Key: req.file.originalname, // Name of the image
Body: req.file.buffer, // Body which will contain the image in buffer format
ACL: "public-read-write", // defining the permissions to get the public link
ContentType: "image/jpeg" // Necessary to define the image content-type to view the photo in the browser with the link
};
// uplaoding the photo using s3 instance and saving the link in the database.
s3.upload(params, (error, data) => {
if (error) {
res.status(500).json({ "message": "Something wrong when the image is being uploded..." }) // if we get any error while uploading error message will be returned.
}
// If not then below code will be executed
console.log(data); // this will give the information about the object in which photo is stored
console.log(req.body.cardContent);
con.query("INSERT INTO Cards (Msgs, BoardID, SenderLastName, SenderFirstName, ImageURL) VALUES ?",
[[[req.body.cardContent, req.body.BoardID, req.body.senderLastName, req.body.senderFirstName, data.Location]]],
function (err, result, fields) {
if (err) {
console.log(err);
res.status(500).json({ "message": "Something wrong when card message is being uploded..." })
};
}
);
res.status(200).json({ "message": "Card message has been written." });
});
})
/*
// creating the storage variable to upload the file and providing the destination folder,
// if nothing is provided in the callback it will get uploaded in main directory
const storage = multer.memoryStorage({
destination: function (req, file, cb) {
cb(null, '')
}
})
// below variable is define to check the type of file which is uploaded
const filefilter = (req, file, cb) => {
if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/jpg') {
cb(null, true)
} else {
cb(null, false)
}
}
// defining the upload variable for the configuration of photo being uploaded
const upload = multer({
storage: storage,
limits: { fileSize: 1000000 },
fileFilter: filefilter
});
// now how to handle the post request and to upload photo (upload photo using the key defined below in upload.single ie: productimage )
app.post('/upload-image', upload.single('cardImage'), (req, res) => {
console.log(req.file) // to check the data in the console that is being uploaded
// Definning the params variable to uplaod the photo
const params = {
Bucket: process.env.AWS_BUCKET_NAME, // bucket that we made earlier
Key: req.file.originalname, // Name of the image
Body: req.file.buffer, // Body which will contain the image in buffer format
ACL: "public-read-write", // defining the permissions to get the public link
ContentType: "image/jpeg" // Necessary to define the image content-type to view the photo in the browser with the link
};
// uplaoding the photo using s3 instance and saving the link in the database.
s3.upload(params, (error, data) => {
if (error) {
res.status(500).send({ "err": error }) // if we get any error while uploading error message will be returned.
}
// If not then below code will be executed
console.log(data); // this will give the information about the object in which photo is stored
/*
// saving the information in the database.
const product = new Product({
name: req.body.name,
price: req.body.price,
productImage: data.Location
});
product.save()
.then(result => {
res.status(200).send({
_id: result._id,
name: result.name,
price: result.price,
productImage: data.Location,
})
})
.catch(err => {
res.send({ message: err })
})
})
res.json({ "mesg": "succeeded" })
});
})
*/
app.listen(port, (err) => {
if (err) console.log("Error in server setup")
console.log(`Server listening on Port ${port}`);
})