forked from frenZone/frenZone
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
99 lines (83 loc) · 2.41 KB
/
server.js
File metadata and controls
99 lines (83 loc) · 2.41 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
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
//Essentials
const express = require('express');
const app = express();
const bp = require('body-parser');
const path = require('path');
const fs = require('fs');
const http = require('http');
const request = require('request');
const webpack = require('webpack');
const webpackMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const config = require('./webpack.config.js');
const api = require('./routes/api');
const db = require('./models');
const User = db.User;
const Photo = db.Photo;
const Location = db.Location;
app.use(express.static('./src/public'));
app.use(bp.urlencoded({extended : true}));
app.use('/api', api);
const isDeveloping = process.env.NODE_ENV !== 'production';
const port = isDeveloping ? 8080 : process.env.PORT;
if (isDeveloping) {
app.set('host', 'http://localhost');
const compiler = webpack(config);
const middleware = webpackMiddleware(compiler, {
publicPath: config.output.publicPath,
contentBase: 'src',
stats: {
colors: true,
hash: false,
timings: true,
chunks: false,
chunkModules: false,
modules: false,
},
});
const response = (req, res) => {
res.write(middleware.fileSystem.readFileSync(path.resolve(__dirname, 'dist/index.html')));
res.end();
};
app.use(middleware);
app.use(webpackHotMiddleware(compiler));
app.get('*', response);
} else {
app.use(express.static(`${__dirname}/dist`));
app.get('*', (req, res) => {
res.write(
fs.readFileSync(path.resolve(__dirname, 'dist/index.html'))
);
});
}
app.get('/write', (req, res) => {
User.Create({
id: data.data[i].user.username,
fullname: data.data[i].user.full_name,
profilePicture: data.data[i].user.profile_picture
});
Location.Create({
id: data.data[i].location.id,
name: data.data[i].location.name,
latitude: data.data[i].location.latitude,
longitude: data.data[i].location.longitude
});
Photo.Create({
id: data.data[i].id,
description: data.data[i].caption.text,
url: data.data[i].images.low_resolution,
locationID: data.data[i].location.id,
UserID: data.data[i].user.id
});
});
const onStart = (err) => {
if (err) {
throw new Error(err);
}
console.info(
`==> 🌎 Listening on port ${port}. ` +
`Open up http://localhost:${port}/ in your browser.`
);
};
app.listen(port, 'localhost', onStart);
module.exports = app;