-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
92 lines (71 loc) · 2.59 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* eslint no-console: 0 */
const path = require('path');
const express = require('express');
const webpack = require('webpack');
const webpackMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const config = require('./webpack.config.js');
const isDeveloping = process.env.NODE_ENV !== 'production';
const port = isDeveloping ? 3000 : process.env.PORT;
const app = express();
const bodyParser = require('body-parser');
const api = require('./db/controllers/api');
const productSchema = require('./db/schema/product');
const Products = require('./db/models/product');
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/shop');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log('MONGO CONNECTED');
});
if (isDeveloping) {
app.get('/api/products', api.showAllProducts);
app.get('/api/products/:id', api.showProduct);
app.post('/api/products', api.addProduct);
app.put('/api/products/:id', api.updateProduct);
app.delete('/api/products/:id', api.deleteProduct);
app.get('/api/ui/categories', api.getCategories);
app.post('/api/ui/categories', api.updateCategories);
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
}
});
app.use(middleware);
app.use(webpackHotMiddleware(compiler));
app.get('*', function response(req, res) {
res.write(middleware.fileSystem.readFileSync(path.join(__dirname, 'dist/index.html')));
res.end();
});
} else {
app.use(express.static(__dirname + '/dist'));
app.get('/products', api.showAllProducts);
app.get('/products/:id', api.showProduct);
app.post('/products', api.addProduct);
app.put('/products/:id', api.updateProduct);
app.delete('/products/:id', api.deleteProduct);
app.get('/ui/categories', api.getCategories);
app.post('/ui/categories', api.updateCategories);
app.get('*', function response(req, res) {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
}
app.listen(port, 'localhost', function onStart(error) {
if (error) {
console.log(error);
}
console.info('==> 🌎 Listening on port %s. Open up http://localhost:%s/ in your browser.', port, port);
});