-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (37 loc) · 1.25 KB
/
index.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
/**
* Basic Express.js app to serve the demo for N-API
*/
const express = require('express');
const editor = require('./build/Release/Edit.node');
const bodyParser = require('body-parser');
const fs = require('fs');
const app = express();
var http = require('http');
var https = require('https');
// Keys for SSL
const privateKey = fs.readFileSync('key.pem', 'utf8');
const certificate = fs.readFileSync('cert.pem', 'utf8');
var credentials = { key: privateKey, cert: certificate };
// Provide index.html on the basic request
app.get('/', (req, res) => {
res.sendFile(__dirname + "/index.html");
});
// The only API - post to perform the image editing.
app.post('/', bodyParser.text({ limit: '50mb' }), (req, res) => {
const requestBuffer = Buffer.from(req.body.split(",")[1], 'base64');
if (requestBuffer.length === 0) {
return res.status(400).send("No image supplied");
}
editor.edit(requestBuffer, (err, buffer) => {
if (buffer) {
res.send("data:image/jpeg;base64," + buffer.toString('base64'));
} else {
res.status(400).send(err);
}
});
});
// Launch the http and http servers.
const httpServer = http.createServer(app);
const httpsServer = https.createServer(credentials, app);
httpServer.listen(8080);
httpsServer.listen(8443);