-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
45 lines (35 loc) · 1.28 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
"use strict";
let express = require('express'),
compression = require('compression'),
products = require('./server/products'),
database = require('./server/database.js'),
app = express();
try {
console.log('Initializing database module');
database.initialize();
} catch (err) {
console.error(err);
process.exit(1); // Non-zero failure code
}
app.set('port', process.env.PORT || 5000);
app.use(compression());
app.use('/', express.static(__dirname + '/www'));
// Adding CORS support
app.all('*', function (req, res, next) {
// Set CORS headers: allow all origins, methods, and headers: you may want to lock this down in a production environment
res.header("Access-Control-Allow-Origin", "*");
//res.header("Access-Control-Allow-Methods", "GET, PUT, PATCH, POST, DELETE");
res.header("Access-Control-Allow-Methods", "GET");
res.header("Access-Control-Allow-Headers", req.header('access-control-request-headers'));
if (req.method === 'OPTIONS') {
// CORS Preflight
res.send();
} else {
next();
}
});
app.get('/products', products.findAll);
app.get('/product/:lid', products.findByProdId);
app.listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});