-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
76 lines (62 loc) · 2.06 KB
/
server.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
const express = require('express');
const compression = require('compression');
const path = require('path');
const bodyParser = require('body-parser');
const cloudinary = require('cloudinary');
/*** Webpack imports ***/
const webpack = require('webpack');
const config = require('./webpack.config.js');
const fs = require('fs');
var app = express();
const compiler = webpack(config);
const webpackOptions = {
publicPath: config.output.publicPath,
quiet: false,
// hides all the bundling file names
noInfo: true,
// adds color to the terminal
stats: {
colors: true
}
};
const isProduction = process.env.NODE_ENV === 'production';
const staticPath = path.join(__dirname, 'build');
// compress all responses
app.use(compression());
// set higher transfer limits
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: false }));
// Check if running in production
if (!isProduction) {
// use the webpackmiddleware to serve up the transpiled files
const webpackMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
app.use(webpackMiddleware(compiler, webpackOptions));
app.use(webpackHotMiddleware(compiler));
// send the index.html at the root of the project.
// Reason is that it looks for the bundle.js and stylesheet in the build
// folder, where the index.html in app/ just looks at the same level.
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
} else {
app.use(express.static(staticPath))
.get('*', (req, res) => {
res.sendFile(path.join(staticPath, 'index.html'));
});
}
app.all('/img', (req, res) => {
const name = req.body.name;
const imageUrl = req.body.imageUrl;
const options = {
public_id: name,
cloud_name: 'dxmist0g2'
};
cloudinary.uploader.unsigned_upload(
imageUrl,
'normal',
(result) => console.log(result),
options
);
});
module.exports = app;