-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathserver.js
34 lines (26 loc) · 1020 Bytes
/
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
"use strict";
let express = require('express'),
compression = require('compression'),
products = require('./server/products'),
app = express();
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-Headers", req.header('access-control-request-headers'));
if (req.method === 'OPTIONS') {
// CORS Preflight
res.send();
} else {
next();
}
});
app.get('/products', products.findAll);
app.get('/products/:id', products.findById);
app.listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});